Swapping variables without a temporary

This is from the first recipe in my brand-new Python cookbook. It’s quite obvious, but it hasn’t really struck me before. Well doh.

In most languages, swapping the values in two variables means using an intermediate temporary variable, e.g.:

<br /> int a = 1;<br /> int b = 2;<br /> int temp;<br /> temp = a;<br /> a = b;<br /> b = temp;<br />

With the tuple packing and unpacking in Python however, we don’t need no steenking temporary variables!

<br /> a = 1<br /> b = 2<br /> b, a = a, b<br />

Obviously this principle scales to any number of variables, e.g.:

<br /> a, b, c, d = d, c, b, a<br />

*sniff* That’s just so sweet.

comments powered by Disqus