More on swapping two variables without using the third one

No matter what you do, sometimes you just need to swap some values – I guess you did it thousand times. And yes, I know – this problem is trivial. In Python it’s completely trivial. You just swap two variables in the most “natural” way:

1
a, b = b, a

instead of doing it this way:

1
2
3
tmp = a
a = b
b = tmp

Beautiful! This is why we love Python, isn’t it?

That’s the moment where I should end this post, if it was about doing it in a “pythonic” way only. But it’s not – it’s about the ideas, not the solutions in any specific language. So…

Continue reading