Problem: Given two variables
b. Use a single line of Python code to swap the variables to assign the value of
b and the value of
Example: Say, you have to integers
b=42. You want to swap the variables so that
a = 21 b = 42 # SWAP MAGIC HERE print(a, b) # 42 21
How to swap to variables as a Python one-liner?
Let’s dive into the best ways to swap integers!
To swap two variables a and b, use the multiple assignment expression
a, b = b, a that assigns the value of
b and the value of
a = 21 b = 42 # Swap One-Liner a, b = b, a # Print the result to the shell print(a, b) # 42 21
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, ais 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
bpoints in memory (here:
42). The second tuple value is the object to which the variable
apoints in memory (here:
21). In our case, the memory now contains a new tuple object
- 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
anow points to the first tuple value (here:
42). The variable name
bnow points to the second tuple value (here:
- This way, the tuple is unpacked to the variables on the left-hand side. Variables
bhave 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
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.