Multiple Assignment and Simultaneous Binding
Tuple unpacking (a, b = b, a) is the fastest and most Pythonic way to swap two variables
Why Swapping Needs Care
Swapping means exchanging the values held by two variables. This is useful when sorting numbers, exchanging player scores, or reordering data. A careless sequential assignment can lose one of the original values, but Python provides a single statement that performs the exchange safely.
What do you think happens?
Suppose a is 10 and b is 20. What will a and b contain after a, b = b, a?
Reveal answer
Answer: a is 20 and b is 10
Python first captures the current values of b and a in that order, then assigns those captured values to a and b.
The Two-Phase Assignment
The expression a, b = b, a works because Python handles it in two phases. First, Python evaluates the complete right side and creates a temporary tuple containing the current value of b followed by the current value of a. Second, Python unpacks those two values and binds them to a and b on the left side. Because both original values are captured before either variable is reassigned, neither value is lost.
a = 10 b = 20 a, b = b, a
A Complete Swap Trace
Exchange Two Scores
Exchange the values of player_one_score and player_two_score without writing an explicit temporary variable.
Starting values: player_one_score is 45 and player_two_score is 60.
Write the values in reverse order: Place player_two_score first and player_one_score second on the right side: player_one_score, player_two_score = player_two_score, player_one_score.
Evaluate the right side: Python captures the current values as 60 and 45 before assigning either result.
Unpack to the left side: The first captured value goes to player_one_score, and the second goes to player_two_score.
player_one_score is 60 and player_two_score is 45.
Why the Idiom Is Preferred
| Tuple unpacking | Temporary-variable method |
|---|---|
| a, b = b, a | Uses several explicit assignment steps |
| No explicitly written temporary variable | Requires a temporary holding variable to preserve an original value |
| Concise and readable | More verbose |
| Python optimizes this common pattern and executes it more efficiently | The source describes tuple unpacking as less efficient than this pattern |
Mistakes That Break a Swap
Using sequential assignment without preserving the first value
After the first assignment, a contains the original value of b. The original value of a has been lost, so the second assignment cannot recover it.
Fix:
Use a, b = b, a so Python captures both original values before either variable is reassigned.Forgetting the comma syntax
The left side no longer names two variables for unpacking, so the statement does not express the intended two-variable swap.
Fix:
Write two targets separated by a comma: a, b = b, a.Writing the right-side values in the wrong order
The values are assigned in their original order, so this does not exchange them.
Fix:
Reverse the right-side order: a, b = b, a.Accidentally rotating more than two variables
This statement moves three values according to a three-variable order rather than performing a simple two-variable swap.
Fix:
For a two-variable swap, use exactly two targets and place their names in reverse order on the right side.
Practice the Binding Order
Write a Python statement that swaps the values of first_item and second_item. Then explain which original value is evaluated first on the right side and where it is assigned on the left side.
Hints
- Use two names on the left side separated by a comma.
- Place the names in reverse order on the right side.
- The first value evaluated on the right side is the current value of second_item.
Debug this attempted swap: a = 3; b = 8; a = b; b = a. Identify the original value that is lost and rewrite the code using simultaneous binding.
Hints
- Trace the values immediately after the first assignment.
- The original value of a must be captured before a changes.
- The corrected form places b first on the right side and a second.
The Pythonic Pattern
- For a two-variable swap, write a, b = b, a. Python evaluates the complete right side first, preserving both original values in an internal temporary tuple. It then unpacks those values into the left-side variables. This approach is concise, readable, and described in the source as faster and more efficient than the traditional temporary-variable method. When debugging, check the comma syntax, the order of the names, and whether a sequential assignment has accidentally overwritten an original value.
Key Takeaways
- Tuple unpacking swaps two values with the syntax a, b = b, a.
- Python evaluates the right side completely before assigning to the left side.
- The current value of b becomes the new value of a, and the current value of a becomes the new value of b.
- The tuple-unpacking pattern is concise, readable, and described as more efficient than the traditional temporary-variable approach.
- Common errors include sequential assignment without a temporary value, missing comma syntax, incorrect order, and accidentally rotating more than two variables.