How to Swap Two Variables in One Line Python?

Rate this post

Problem: Given two variables a and b. Use a single line of Python code to swap the variables to assign the value of a to b and the value of b to a.

Example: Say, you have to integers a=21 and b=42. You want to swap the variables so that a=42 and b=21.

a = 21
b = 42
# SWAP MAGIC HERE
print(a, b)
# 42 21

How to swap to variables as a Python one-liner?

Swap Variables Python

Let’s dive into the best ways to swap integers!

Swap Integers

To swap two variables a and b, use the multiple assignment expression a, b = b, a that assigns the value of a to b and the value of b to a.

a = 21
b = 42

# Swap One-Liner
a, b = b, a

# Print the result to the shell
print(a, b)
# 42 21

Explanation

The Python interpreter evaluates every expression from left to right. Python realizes that in order to assign the value to the variables, it must first evaluate the right-hand-side of the equation. So, here are the steps it takes to determine the result of the line a, b = b, a:

  • The right side b, a is short for (b, a) and, thus, it creates a tuple in memory with two values. The first tuple value is the object to which the variable b points in memory (here: 42). The second tuple value is the object to which the variable a points in memory (here: 21). In our case, the memory now contains a new tuple object (42, 21).
  • Python now assigns this tuple to the list of variables defined on the left-hand side of the equation (here: a, b = ...). The variable name a now points to the first tuple value (here: 42). The variable name b now points to the second tuple value (here: 21).
  • This way, the tuple is unpacked to the variables on the left-hand side. Variables a and b have been swapped.

Do It Yourself: You can see this code and the memory in action in the interactive memory visualizer:

Click “Next” to view how the memory objects unfold.

Does this also work to swap other objects like lists or strings?

How to Swap Lists, Tuples, and Other Objects

Let’s try out in our interactive code shell:

Exercise: Print the result of the variables a and b before and after swapping. Can you swap any object using Python’s multiple assignment feature?

You can see this code and the memory in action in the interactive memory visualizer:

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!