October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Swap Three Variables in Python Without a Temporary Variable

One line, a, b, c = b, c, a, rotates three Python variables without a temporary variable. Here is why it works and when to use it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

“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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.