As we all know, the pythonic way to swap the values of two items a
and b
is
a, b = b, a
and it should be equivalent to
b, a = a, b
However, today when I was working on some code, I accidentally found that the following two swaps give different results:
nums = [1, 2, 4, 3]
i = 2
nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
print(nums)
# [1, 2, 4, 3]
nums = [1, 2, 4, 3]
i = 2
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
print(nums)
# [1, 2, 3, 4]
What is happening here? I thought in a Python swap the two assignments happen simultaneously and independently.
See also Multiple assignment and evaluation order in Python regarding the basic semantics of this kind of assignment.
See also Multiple assignment semantics regarding the effect and purpose of parentheses on the left-hand side of a multiple assignment.
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.