Swapping two variables is one of those programming exercises that seems simple… until someone challenges you to do it without a temporary variable. You might see this in a classroom, in a tech interview, or even in coding competitions.
In this post, we’ll cover three ways to swap variables in Python without the use of a temporary variable. Whether you’re here for the interview tip or you’re just bored, you’ll leave with a solid understanding of each method.
How To Swap Variables Using A Temporary Variable?
When asked this question, you probably reached for a temporary variable without even thinking about it:
a = 5
b = 10
temp = a
a = b
b = temp
That works perfectly fine, and for the most part, it is just the simplest way to swap variables in most programming languages.
Method 1: XOR Bitwise Swap
Here is the method that you should be using when answering this question in an interview:
a = 5
b = 10
a = a ^ b
b = a ^ b
a = a ^ b
nterview process. The better you understand and can explain it, the more impressive it sounds in an interview. After all, the leet code style interview question is all about seeing if you can logically break down problems and apply basic programming knowledge to solve them.
Before we dive into breaking down the swap trick using XOR, let’s talk about what XOR actually is.
XOR stands for “exclusive or”, and it’s a bitwise operation that can be executed in Python by using the caret symbol ^
.
It compares two numbers bit by bit, and for each bit:
If the bits are different, it returns
1
If the bits are the same, it returns
0
Step-by-Step Breakdown:
a = a ^ b # 0101 ^ 1010 = 1111
b = a ^ b # 1111 ^ 1010 = 0101
a = a ^ b # 1111 ^ 0101 = 1010
XOR Properties to know
a ^ a = 0 -> performing XOR on something with itself gives zero
a ^ 0 = a -> performing XOR on something with zero gives the original value
a ^ b ^ b = a -> you can cancel out one of the values by performing XOR on something twice
Method 2: Swapping with Arithmetic (Only for Numbers)
The second method we are going to look at swaps the variables using raw arithmetic.
a = 5
b = 10
a = a + b
b = a - b
a = a - b
Step-by-Step Breakdown:
Let’s walk through what’s actually happening, assuming a = 5
and b = 10
.
a = a + b -> a = 15
b = a — b -> b = 15–10 = 5
a = a — b -> a = 15–5 = 10
And just like that, a
and b
have swapped.
The caveats to this method are that it only works with numbers, and even then, numbers that won’t produce an integer overflow when performing arithmetic operations. Usually, this isn’t an issue with Python.
Method 3: Python Tuple Unpacking
Now we get to the “pythonic” way of doing this task — tuple unpacking.
a = 5
b = 10
a, b = b, a
This might look like some Python-specific shortcut, but there’s more going on under the hood. Let’s break it down:
Python evaluates the right-hand side as a tuple:
(b, a)
Python unpacks the tuple and assigns the first value to
a
and the second tob
This specific situation is called tuple unpacking, and it works with more than just swapping two variables. You can unpack values from a list, a tuple, or even a function that returns multiple values.
Note: If you try this in a language like C or Java, you’ll run into errors; those languages don’t support multiple assignment like Python does.