Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b? Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b? python-3.x python-3.x

Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b?


From python.org

Assignment of an object to a target list, optionally enclosed in parentheses or square brackets, is recursively defined as follows.

...

  • Else: The object must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets.

So I interpret that to mean that your assignment

nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]

is roughly equivalent to

tmp = nums[nums[i]-1], nums[i]nums[i] = tmp[0]nums[nums[i] - 1] = tmp[1]

(with better error-checking, of course)

whereas the other

nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]

is like

tmp = nums[i], nums[nums[i]-1]nums[nums[i] - 1] = tmp[0]nums[i] = tmp[1]

So the right-hand side is evaluated first in both cases. But then the two pieces of the left-hand side are evaluated in order, and the assignments are done immediately after evaluation. Crucially, this means that the second term on the left-hand side is only evaluated after the first assignment is already done. So if you update nums[i] first, then the nums[nums[i] - 1] refers to a different index than if you update nums[i] second.


This is because evaluation -- specifically at the left side of the = -- happens from left to right:

nums[i], nums[nums[i]-1] =

First nums[i] gets assigned, and then that value is used to determine the index in the assignment to nums[nums[i]-1]

When doing the assignment like this:

nums[nums[i]-1], nums[i] =

... the index of nums[nums[i]-1] is dependent on the old value of nums[i], since the assignment to nums[i] still follows later...


This happens according to the rules:

  • The right hand side is evaluated first
  • Then, each value of the left hand side gets its new value, from left to right.

So, with nums = [1, 2, 4, 3], your code in the first case

nums[2], nums[nums[2]-1] = nums[nums[2]-1], nums[2]

is equivalent to:

nums[2], nums[nums[2]-1] = nums[nums[2]-1], nums[2]nums[2], nums[nums[2]-1] = nums[3], nums[2]nums[2], nums[nums[2]-1] = 3, 4

and as the right hand side is now evaluated, the assignments are equivalent to:

nums[2] = 3nums[nums[2]-1] = 4nums[2] = 3nums[3-1] = 4nums[2] = 3nums[2] = 4

which gives:

print(nums)# [1, 2, 4, 3]

In the second case, we get:

nums[nums[2]-1], nums[2] = nums[2], nums[nums[2]-1]nums[nums[2]-1], nums[2] = nums[2], nums[3]nums[nums[2]-1], nums[2] = 4, 3nums[nums[2]-1] = 4nums[2] = 3nums[4-1] = 4nums[2] = 3nums[3] = 4nums[2] = 3print(nums)# [1, 2, 3, 4]