Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b?
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]
This is mind-boggling to me. Can someone explain to me what happened here? I thought in a Python swap the two assignments happen simultaneously and independently.
Solution 1:
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.
Solution 2:
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...
Solution 3:
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] = 3
nums[nums[2]-1] = 4
nums[2] = 3
nums[3-1] = 4
nums[2] = 3
nums[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, 3
nums[nums[2]-1] = 4
nums[2] = 3
nums[4-1] = 4
nums[2] = 3
nums[3] = 4
nums[2] = 3
print(nums)
# [1, 2, 3, 4]
Solution 4:
On the left hand side of your expression you are both reading and writing nums[i], I dunno if python gaurantees processing of unpacking operations in left to right order, but lets assume it does, your first example would be equivilent to.
t = nums[nums[i]-1], nums[i] # t = (3,4)
nums[i] = t[0] # nums = [1,2,3,3]
n = nums[i]-1 # n = 2
nums[n] = t[1] # nums = [1,2,4,3]
While your second example would be equivilent to
t = nums[i], nums[nums[i]-1] # t = (4,3)
n = nums[i]-1 # n = 3
nums[n] = t[0] # nums = [1,2,4,4]
nums[i] = t[0] # nums = [1,2,3,4]
Which is consistent with what you got.