I wrote a simple code two swap two items in a list, using the very basic:
a, b = b, a
the code looks like this:
nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]
the above swap results in an infinite loop where the items are duplicated in the list thus failing to swap. But modifying it to the following works:
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
Is the swap not working because of the order of the variables for swapping.
If so, how is it logical? What am I missing?