Search code examples
python-3.xlistindexingswap

very strange behavior when swap two elements in the list in python3


I have a list

a = [-1,4,3,1]

we have

a[1] = 4
a[3] = 1

now I want to swap a[1] and a[3]

The following code work very well:

a[1], a[3]  = a[3], a[1]

But If I use:

a[1], a[a[1]-1] = a[a[1]-1], a[1]

It gives me:

 a = [4,1,3,1]

The answer is not correct. What happen here since we know a[1] - 1 = 3. Any thought, thanks.


Solution

  • This is fundamentally the same question as Multiple assignment and evaluation order in Python with a superficial wrinkle because of the convoluted indexing.

    The things to bear in mind here:

    • The right side is evaluated completely before the left side is evaluated at all

    • The tuple assignments are evaluated completely from left to right

    The docs state this (albeit sparely) here: https://docs.python.org/3/reference/expressions.html#evaluation-order

    Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

    Here I illustrate these rules as applied to your situation:

    State of a Evaluation of expression Comment [-1, 4, 3, 1] a[1], a[a[1]-1] = a[a[1]-1], a[1] <start> [-1, 4, 3, 1] a[1], a[a[1]-1] = a[4-1], a[1] a[1] resolves to 4 [-1, 4, 3, 1] a[1], a[a[1]-1] = a[3], a[1] 4-1 is 3 [-1, 4, 3, 1] a[1], a[a[1]-1] = 1, a[1] a[3] resolves to 1 [-1, 4, 3, 1] a[1], a[a[1]-1] = 1, 4 a[1] resolves to 4 [-1, 1, 3, 1] a[a[1]-1] = 4 a[1] = 1 assigned [-1, 1, 3, 1] a[1-1] = 4 a[1] resolves to 1 [-1, 1, 3, 1] a[0] = 4 1-1 is 0 [4, 1, 3, 1] a[0] = 4 assigned