why L[3:1]=['?'] become L.insert(3,'?')

Richard Thomas chardster at gmail.com
Mon Oct 4 11:00:38 EDT 2010


On Oct 4, 3:26 pm, sd44 <sd44s... at yeah.net> wrote:
> In <learning python>
> part III
> how does Python handle it if you try to extract a sequence in reverse,
> with the lower bound greater than the higher bound (e.g., L[3:1])? Hint:
> try
> assigning to this slice (L[3:1]=['?']), and see where the value is put.
>
> >>> L=[1,2,3,4]
> >>> L[3:1]
> []
> >>> print L
> [1, 2, 3, 4]
> >>> L[3:1]=['?']
> >>> print L
>
> [1, 2, 3, '?', 4]

L1[a:b] = L2 removes all elements in L1 between a and b and inserts L2
at the start index. If 0 <= a < b then that removes no elements and
inserts the new ones. Seems like the most sensible and consistent
behaviour to me.

Chard.



More information about the Python-list mailing list