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.

two variables. If you use complex expressions order of evaluation comes into play.17more comments