Python supports 1-line A/B swap (A , B = B , A), and we would expect (B, A = A, B) would lead to the same result. However, I met a strange behavior when dealing with a list entry. I simplify my finding in the code below:
nums = [0, 0, 0, 0]
n = 2
print(nums, "expected case")
nums[n - 1], n = n, nums[n - 1]
print(n, nums)
nums = [0, 0, 0, 0]
n = 2
print(nums, "unexpected case")
n, nums[n - 1] = nums[n - 1], n
print(n, nums)
Below are output from executing the program above:
[0, 0, 0, 0] expected case
0 [0, 2, 0, 0]
[0, 0, 0, 0] unexpected case
0 [0, 0, 0, 2]
Question: why the order change above leads to different result? It appears that in the unexpected case, n gets assigned to 0 first, before nums[-1] gets updated.