Pair and Unpack Values
Keep labels, positions, and readings aligned with small tuples, unpacking, enumerate, and zip, including the shorter-input boundary.
A reading becomes easier to interpret when its label stays beside it. A small fixed pair can keep those two values together:
measurement = ("north", 21)
This pair is a tuple. Like a list and a string, it is ordered. Unlike the result lists built in Lesson 2, this tuple represents one fixed grouping rather than a collection the program will grow.
Unpack a Fixed Pair
Selection by index works:
When both positions have clear roles, unpacking names them together:
Python assigns the first tuple item to sensor and the second to reading.
The number of names must match the number of items. This chapter uses ordinary
two-item pairs and does not need special tuple syntax.
Q1. Unpack one measurement
What values do sensor and reading receive?
sensor, reading = ("east", 19)
Select one choice, then check.
HintKeep the positions aligned
"east" is the first tuple item.
SolutionAssign one item to each name
sensor receives "east", and reading receives 19.
Pair Each Value with Its Position
A loop already visits the readings in order. enumerate supplies each
zero-based position together with its value:
The output is:
0 18
1 21
2 20
On each iteration, enumerate produces a pair. The loop header unpacks that
pair into index and reading. This is clearer than maintaining and updating a
separate counter when the required positions are the ordinary sequence indexes.
Q2. Trace positions with enumerate
What does the second iteration display?
Select one choice, then check.
HintBegin at zero
The first pair is (0, 12).
SolutionThe second pair is one and fifteen
The pairs are (0, 12), (1, 15), and (2, 17). The second iteration
displays 1 15.
Pair Two Sequences with Zip
When labels and readings are stored separately, zip pairs items at matching
positions:
The loop visits these pairs in order:
| Position | Sensor | Reading | Unpacked pair |
|---|---|---|---|
| 0 | "north" | 18 | sensor="north", reading=18 |
| 1 | "east" | 21 | sensor="east", reading=21 |
| 2 | "west" | 20 | sensor="west", reading=20 |
zip stops when the shorter sequence ends:
This processes north with 18 and east with 21. It does not report an error
for the unmatched "west". That behavior is useful only when stopping at the
shorter input is acceptable.
When losing an unmatched item would be wrong, check the lengths before pairing:
The check states the program's requirement explicitly. zip still performs
ordinary position-by-position pairing once the lengths are known to match.
Q3. Protect an aligned report
The report must not silently lose a sensor or reading. Add the equal-length
check so this mismatched input displays Sensor and reading counts differ
instead of a partial report.
Editable Python
Ready to run.
HintCheck before pairing
Use if len(sensors) != len(readings) for the message and place the existing
loop in the else branch.
SolutionReject unequal lengths
Tuples keep a small fixed group together, unpacking gives each position a
name, enumerate pairs values with indexes, and zip aligns separate
sequences until the shorter one ends. With paired iteration visible, the
next lesson can replace familiar summary loops with checked standard
operations.