Use tuple unpacking: a, b, c = b, c, a. After this one statement, a holds the old value of b, b holds the old value of c, and c holds the old value of a. No temporary variable appears in your code.
The code
a, b, c = 1, 2, 3
a, b, c = b, c, a
# a == 2, b == 3, c == 1
This is a rotation, not a full reversal or a sort. The mapping is a ← old b, b ← old c, c ← old a. If you need the opposite direction, write a, b, c = c, a, b. If you need sorting, do that explicitly instead of changing the assignment order.
Why it works
The comma-separated expression list on the right forms a tuple of the three current values. Python’s assignment statement rules evaluate the right-hand side completely before assigning to any target on the left. The original values are therefore still available when the assignments happen, and nothing is overwritten too early. The three values are then unpacked into a, b and c from left to right.
Parentheses are optional here, so a, b, c = (b, c, a) means the same thing.
#1 Best Overall
“Without a temporary variable” describes your source code. The Python reference defines the assignment behavior. It does not promise anything about memory allocation or what the interpreter does internally, so don’t read the phrase as “no intermediate object exists.”
Tuple unpacking versus a temporary variable
| Approach | Code | Strength |
|---|---|---|
| Tuple unpacking | a, b, c = b, c, a |
Shortest, and the idiomatic Python form. Pylint’s R1712 check recommends it for swaps. |
| Explicit temporary | t = a; a = b; b = c; c = t |
Each step is visible, which helps when teaching or stepping through a debugger. |
The sources support unpacking on clarity grounds only. No speed or memory measurements back either option, so don’t choose between them on performance. Pylint’s R1712 documentation says: “You do not have to use a temporary variable in order to swap variables.” It adds that tuple unpacking makes the intention clearer.
Rank #2
Why not XOR?
The XOR trick is common in other languages. In Python, ^ is bitwise XOR, and it needs integer operands or objects that implement the matching special methods. It fails on strings, lists and most other objects. Tuple unpacking works with any values, and it is easier to read.
Further reading
The Python Cookbook by Alex Martelli and David Ascher (O’Reilly, July 2002) has a section titled “Swapping Values Without Using a Temporary Variable.” It explains the tuple packing and unpacking behind this idiom. The book is old, so check which edition is current before buying.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




