Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I swap two Python values without a temporary variable?

Quick answer

Write a, b = b, a. Python evaluates the values on the right before assigning them to the names on the left, so the names receive each other’s original values. For example, if a is 1 and b is 2, afterward a is 2 and b is 1. Two separate assignments like a = b and then b = a instead overwrite the original value of a. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What’s the straightforward way to swap two values in Python without making a temporary variable?

Geez

Use tuple unpacking:

a, b = b, a

For a concrete example:

a = 1
b = 2
a, b = b, a
print(a, b)  # 2 1

The values on the right are evaluated before assignment to the names on the left, so each name gets the other name’s value from before the statement. [S1] You don’t need a separate temporary name to hold one value while you move the other.

Follow-up question

Why doesn’t a = b followed by b = a do the same thing?

Geez

Because the first statement changes what a refers to before the second statement runs. Suppose a is 1 and b is 2:

a = b  # a is now 2; b is still 2
b = a  # reads the new value of a, so b is 2

The original 1 is no longer available through either name. The two-line version therefore leaves both names with the value 2, rather than exchanging their values. In the unpacking version, Python evaluates the right-hand values before assigning to the left-hand targets, which is why both original values are available for the swap. [S1]

Follow-up question

Can I use the same pattern with different variable names, and how do I check the result?

Geez

Yes. Put the two names you want to exchange on the left, then put them in the opposite order on the right. For example, assuming left and right already hold the values you want to swap:

left, right = right, left

You can check the pattern with distinct starting values:

left = "east"
right = "west"
left, right = right, left
print(left, right)  # west east

After the assignment, left has the former value of right, and right has the former value of left. This small check helps catch a reversed or mistyped name; it does not tell you whether other parts of a program depend on those names.

Follow-up question

What’s a quick way to remember the order when editing an existing example?

Geez

Keep the left-hand names in the order you want to refer to them afterward, and write their values in the order they should receive them on the right. For a two-name swap, the right-hand names are reversed:

first, second = second, first

Before using it, check that both sides have the two intended names and that they appear in opposite order. If you accidentally write first, second = first, second, each name gets its own current value, so nothing is exchanged. The swap behavior described here is Python assignment behavior; don’t assume another language handles the same-looking syntax the same way. [S1]

A practical checklist

  • Identify the two names whose current values should exchange places.

  • Put both names on the left side of one assignment, separated by a comma.

  • Put the same names in reverse order on the right: a, b = b, a.

  • If unsure, try two distinct starting values and inspect both names afterward.

  • Avoid two sequential assignments that overwrite an original value.

A few more useful answers

Does `a, b = a, b` swap the values?

No. Each name receives its own current value, so that statement leaves them unchanged.

Does the same swap syntax apply in every programming language?

Not necessarily. This describes Python’s assignment behavior; check the rules for another language separately.

Why does tuple unpacking avoid losing either original value?

Python evaluates the right-hand values before assigning them to the left-hand targets, so both original values are available for the assignment. [S1]

Sources

References used for this guide.

  1. [S1] Python Language Reference: Assignment statements and unpacking
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work