slicing and parallel assignment: inconsistent behaviour??

Tim Lesher tim at lesher.ws
Thu Jul 18 13:14:24 EDT 2002


tjd at sfu.ca (Toby Donaldson) wrote in message news:<ad32251c.0207172302.12f4ef4f at posting.google.com>...
> This works as I expect. But if I leave out the 10s in the slice
> indices, I get this:
> 
>     >>> A = range(10)
>     >>> A[1:5], A[5:] = A[5:], A[1:5]
>     >>> A
>     [0, 5, 6, 7, 8, 1, 2, 3, 4]
> 
> Where has the 9 gone? 

You're overwriting a[5] twice.  

Look at it step by step:
>>> a = range(10)

You next want to do a[1:5] = a[5:].  But those are different-sized
slices:
>>> a[1:5]
[1, 2, 3, 4]
>>> a[5:]
[5, 6, 7, 8, 9]

So a[1:5] gets replaced with [5, 6, 7, 8, 9], and a[5:], _which starts
at the 9_, gets replaced with [1, 2, 3, 4].

--
Tim Lesher
tim at lesher.ws



More information about the Python-list mailing list