Variable Swapping Using Tuple Assignment
Unpacking is a syntactic feature that assigns multiple variables from a sequence in one statement, equivalent to index-based assignment.
The One-Line Swap
Tuple assignment lets one statement assign values to multiple variables. The same unpacking pattern can swap two values: the value currently associated with a is assigned to b, while the value currently associated with b is assigned to a.
What do you think happens?
After these statements run, what values are associated with a and b? a, b = 10, 20 a, b = b, a
Reveal answer
Answer: a is 20 and b is 10
The second statement unpacks the two values in the order b, a into the targets a, b. The value from b goes to a, and the value from a goes to b.
Position Controls Assignment
Unpacking assigns sequence elements to variables by position. In x, y = sequence, the first element is assigned to x and the second element is assigned to y. The number of variables must exactly match the number of elements being unpacked.
values = (7, 12) x, y = values
Unpacking and Index Assignment
Unpacking is a syntactic feature that assigns multiple variables from a sequence in one statement. It is equivalent to assigning the values by their indexes.
| Unpacking form | Index-based form |
|---|---|
| x, y = sequence | x = sequence[0]; y = sequence[1] |
| first, second = values | first = values[0]; second = values[1] |
Tuples Lists and Strings
Unpacking works uniformly with tuples, lists, strings, and any sequence type. The source sequence can differ, but the assignment rule remains positional: the first element goes to the first variable, the second element goes to the second variable, and so on.
first is 3 and second is 4
left is 8 and right is 9
letter_one is h and letter_two is iMatching Element Counts
The number of target variables must exactly match the number of elements in the source sequence. If there are too many or too few targets, unpacking cannot complete and causes a ValueError.
Using fewer variables than elements
There are two target variables but three source elements.
Fix:
Use three variables, such as a, b, c = [1, 2, 3].Using more variables than elements
There are three target variables but only two source elements.
Fix:
Make the number of variables equal to the number of elements.Assuming the source type changes the positional rule
A string is still unpacked by position, just like a tuple or list.
Fix:
Count the string's sequence elements and match them with the target variables.
A Reliable Unpacking Check
- Identify the source sequence.
- Count its elements.
- Count the target variables.
- Match the first source element with the first target, the second with the second, and so on.
- If the counts differ, add or remove target variables before running the assignment.
- For a swap, place the variables in the opposite order on the right side.
Tracing a Three-Element Unpack
Determine the values assigned by first, second, third = [4, 5, 6].
Count the source elements: The list contains three elements: 4, 5, and 6.
Count the targets: There are three target variables: first, second, and third.
Map positions: The first element 4 goes to first, 5 goes to second, and 6 goes to third.
first is 4, second is 5, and third is 6.
Practice the Mapping
For each statement, identify the value assigned to every variable. Then decide whether any statement causes a ValueError. 1. left, right = (2, 9) 2. first, second, third = ["a", "b", "c"] 3. one, two = "cat" 4. score, best = 40, 75 5. score, best = best, score
Hints
- Map elements from left to right.
- Check that the number of variables equals the number of sequence elements.
- For the final statement, read the right side in the order best, score.
1. left is 2 and right is 9.
2. first is a, second is b, and third is c.
3. The statement causes a ValueError because the string provides three elements and there are only two variables.
4. score is 40 and best is 75.
5. score becomes 75 and best becomes 40.Key Takeaways
- Unpacking assigns multiple variables from one sequence in a single statement.
- Assignment follows position: the first element goes to the first variable, the second element goes to the second variable, and so on.
- Unpacking is equivalent to index-based assignment such as x = sequence[0] and y = sequence[1].
- Tuples, lists, strings, and other sequence types can be unpacked using the same positional rule.
- The number of variables must exactly match the number of elements, or a ValueError occurs.
- Writing the variables in reverse order on the right side provides a concise way to swap their values.
Key Takeaways
- Unpacking maps sequence elements to multiple variables by position.
- Tuple assignment can swap two variable values in one statement.
- The same unpacking approach works with tuples, lists, and strings.
- Unpacking corresponds to index-based assignment.
- A mismatch between target variables and source elements causes a ValueError.