Tuples: Immutable Sequences
Unpacking is a syntactic feature that assigns multiple variables from a sequence in one statement, equivalent to index-based assignment.
A Sequence Becomes Several Names
Suppose a sequence contains three elements and you want each element in a separate variable. Unpacking lets one assignment statement perform that mapping. Instead of assigning each position separately, you place several variable names on the left side and the sequence on the right side.
After this statement, x receives the first element, y receives the second, and z receives the third. The original sequence is used as the source of the values; unpacking does not require writing an assignment for each index.
Position Controls Assignment
Unpacking follows position. The first variable receives the first sequence element, the second variable receives the second element, and so on. The sequence can be a tuple, list, string, or another sequence type. The same left-to-right mapping principle applies in each case.
From Unpacking to Indexing
Unpacking is equivalent to index-based assignment. This means that a compact unpacking statement represents the same position-by-position idea as separate assignments that read the sequence at index 0, index 1, and the following positions.
Reading two values by position
Assign the elements of the sequence (8, 13) to first and second using unpacking.
Match positions: The first variable, first, corresponds to sequence position 0, and the second variable, second, corresponds to sequence position 1.
Write the unpacking statement: Place the two variable names on the left and the two-element sequence on the right: first, second = (8, 13).
Relate it to indexing: The same assignment can be understood as first = values[0] and second = values[1] when values refers to the sequence.
first receives 8 and second receives 13.
Three Sequence Forms
The unpacking syntax stays the same when the source is a tuple, list, or string. What changes is the sequence being read. For a tuple or list, the variables receive the sequence elements. For a string, the variables receive its characters by position.
a is 4
b is 9
first_item is red
second_item is blue
left is O
right is K| Source sequence | Unpacking statement | Position-based result |
|---|---|---|
| Tuple | a, b = (4, 9) | a receives the first element; b receives the second |
| List | a, b = [4, 9] | a receives the first element; b receives the second |
| String | a, b = "OK" | a receives the first character; b receives the second |
Counting Before You Unpack
A basic unpacking assignment requires the number of variables to exactly match the number of elements in the sequence. If the counts differ, Python raises a ValueError. Counting both sides before writing the statement is the simplest way to prevent this error.
Using fewer variables than elements
There are three sequence elements but only two variable names.
Fix:
Use three variables, such as first, second, third = (10, 20, 30).Using more variables than elements
There are only two sequence elements but three variable names.
Fix:
Use two variables, such as first, second = (10, 20).Counting variable names incorrectly
The string provides two characters, but the left side requests three values.
Fix:
Use two variables, such as a, b = "OK".
Practice the Mapping
What do you think happens?
What happens when this statement runs? left, middle, right = ["A", "B", "C"]
Reveal answer
Answer: left gets A, middle gets B, and right gets C.
Unpacking works with lists as well as tuples. The three variables match the three list elements by position.
For each statement, decide whether it succeeds. If it succeeds, identify the value assigned to each variable. If it fails, explain the count mismatch. 1. first, second = ("up", "down") 2. a, b, c = "go" 3. one, two = [1, 2, 3] 4. red, blue, green = ["red", "blue", "green"]
Hints
- Count the variables on the left.
- Count the elements or characters on the right.
- When the counts match, map values from left to right.
When debugging an unpacking statement, first identify the source sequence and count its elements. Then count the variable names on the left. If the counts match, trace the assignment by position; if they do not, revise one side before looking for another cause.
The Essential Pattern
- Unpacking assigns multiple variables from one sequence in a single statement.
- Assignment happens by position: the first variable receives the first element, the second receives the second, and so on.
- Tuples, lists, strings, and other sequence types can be unpacked using the same pattern.
- Ordinary unpacking requires the number of variables to exactly match the number of sequence elements; otherwise Python raises a ValueError.
- Unpacking is equivalent to separate index-based assignments, and parentheses around the left-side variables are optional.
Key Takeaways
- Unpacking maps sequence elements to variables by position.
- The same unpacking syntax works with tuples, lists, and strings.
- The variable count and element count must match exactly for ordinary unpacking.
- A ValueError signals that the two counts differ.
- Unpacking provides concise syntax for index-based assignments.