Let's say I want to swap 2 elements in a list by their indexes (it's a snippet from an algorithm problem so looks a little tricky).
>>> nums = [4, 3, 2, 1]
>>> i = 1
>>> nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
>>> nums
[4, 3, 2, 1]
So it doesn't work.
While a swap using constant numbers work as expected.
>>> nums[1], nums[2] = nums[2], nums[1]
>>> nums
[4, 2, 3, 1]
Can anyone help me understand what happens in the line nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
? What's the evaluation/execution order or if I missed anything?