slicing and parallel assignment: inconsistent behaviour??

Toby Donaldson tjd at sfu.ca
Thu Jul 18 03:02:25 EDT 2002


Hi,

I was hoping someone might be able to help me with a problem I've run
into with slices and parallel assignment. I want to swap to segments
of a list. For instance:

    >>> B = range(10)
    >>> B[1:5], B[5:10] = B[5:10], B[1:5]
    >>> B
    [0, 5, 6, 7, 8, 1, 2, 3, 4, 9]

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? 

If I include the 10 on the left side of the assignment, it works as I
expect:

    >>> C = range(10)
    >>> C[1:5], C[5:10] = C[5:], C[1:5]
    >>> C
    [0, 5, 6, 7, 8, 1, 2, 3, 4, 9]

Yet leaving adding the 10 on the right side of the assignment and
leaving it out on the left has the same behaviour as the A example:

    >>> D = range(10)
    >>> D[1:5], D[5:] = D[5:10], D[1:5]
    >>> D
    [0, 5, 6, 7, 8, 1, 2, 3, 4]

Any idea what's going on?

Toby



More information about the Python-list mailing list