Swapping variables is one of the most common beginner exercises in programming. Whether you’re preparing for interviews, coding challenges, or just practicing Python, it’s essential to know the different ways to swap two variables.
In this blog, we’ll explore all the major methods to swap variables in Pythonβranging from the classic to the most Pythonic ways.
πΉ Scenario
Letβs say we have two variables:
glass1 = "milk"
glass2 = "juice"Our goal is to swap their contents so that:
glass1 β "juice"
glass2 β "milk"β 1. Using a Temporary Variable
The most basic method, used in almost every language:
temp = glass1
glass1 = glass2
glass2 = tempπ’ Pros: Easy to understand
π΄ Cons: Requires an extra variable
β 2. Pythonic Way β Tuple Unpacking
Python allows swapping in a single line using tuple unpacking:
glass1, glass2 = glass2, glass1π’ Pros: Clean, readable, no temporary variable needed
π΄ Cons: Might confuse absolute beginners
β 3. Using a List
Swap values using a list and index positions:
glasses = [glass1, glass2]
glasses[0], glasses[1] = glasses[1], glasses[0]
glass1, glass2 = glassesπ’ Pros: Useful when working with list-based data
π΄ Cons: Slightly overkill for just two variables
β 4. Using a Dictionary
Although not common, dictionaries can be used to swap:
glasses = {"g1": glass1, "g2": glass2}
temp = glasses["g1"]
glasses["g1"] = glasses["g2"]
glasses["g2"] = temp
glass1 = glasses["g1"]
glass2 = glasses["g2"]π’ Pros: Good for more than 2 variables
π΄ Cons: Verbose for this simple case
β 5. Using a Custom Swap Function
We can also define a reusable function to swap:
def swap(a, b):
return b, a
glass1, glass2 = swap(glass1, glass2)π’ Pros: Clean logic, reusable
π΄ Cons: Slightly more code for a basic swap
β 6. Using Arithmetic Operations (Not for Strings)
This works only for numbers, not strings like “milk”/”juice”:
a = 5
b = 10
a = a + b
b = a - b
a = a - bπ’ Pros: No extra variables
π΄ Cons: Not suitable for non-numeric types; risk of overflow
β 7. Using Bitwise XOR (For Integers Only)
Also works only for integers:
a = 5
b = 10
a = a ^ b
b = a ^ b
a = a ^ bπ’ Pros: No extra memory
π΄ Cons: Confusing; not for strings or floats
β 8. Using Stack (Advanced)
This can be demonstrated using a list as a stack:
stack = []
stack.append(glass1)
stack.append(glass2)
glass1 = stack.pop()
glass2 = stack.pop()π’ Pros: Simulates real-world stack behavior
π΄ Cons: Over-complicated for simple variable swap
β Summary Table
| Method | Works for Strings | Extra Variable | Pythonic | Line Count |
|---|---|---|---|---|
| Temporary Variable | β | β | β | 3 |
| Tuple Unpacking | β | β | β | 1 |
| List Indexing | β | β | β | 3+ |
| Dictionary | β | β | β | 4+ |
| Custom Function | β | β | β | 2 |
| Arithmetic (only numbers) | β | β | β | 3 |
| Bitwise XOR (integers) | β | β | β | 3 |
| Stack/List push-pop | β | β | β | 3+ |
π§ Final Thoughts
Swapping variables is a fundamental concept that helps you understand memory, variable references, and Pythonβs flexibility. Master all methods to sharpen your understanding, and use the most readable or efficient one based on your context.
π‘ Tip: In real-world Python code, always prefer
glass1, glass2 = glass2, glass1unless you have a special case.
