Python zip: different lengths and strict=True
See why Python zip drops unmatched items, when strict=True raises ValueError, and how to choose zip_longest or validate pairs before processing them.
You loop over two lists together, but the last row never appears. There is no exception: zip() stopped at the end of the shorter list. Before changing the loop, decide what an unmatched value means in your data.
In short
In Python, zip(a, b) produces tuples and stops as soon as an input is exhausted. With strict=True, available since Python 3.10, it raises ValueError if the inputs aren't exhausted together, but only as you iterate. Use itertools.zip_longest() to keep unmatched items and choose a fill value. If processing must wait until the lengths have been validated, consume the entire strict result first.
Why does zip drop the last item?
Each tuple needs one value from every input. Here, C has no matching number, so the result ends after B.
keys = ["A", "B", "C"]values = [1, 2]print(list(zip(keys, values)))print(keys, values)$ python3 pairs.py[('A', 1), ('B', 2)]['A', 'B', 'C'] [1, 2]The lists stay unchanged. zip() doesn't delete C or pad the second list. Its iteration simply produces no tuple for that position. With three inputs, the same rule applies as soon as any one is exhausted.
If the lists contain names and scores for the same people, a length mismatch probably points to an earlier bug. If you deliberately want only their common prefix, stopping at the shorter input may be exactly the right contract.
What should happen to an unmatched value?
Common prefix wanted
zip(a, b)
Unmatched items stay out of the result.
Inputs must match
zip(a, b, strict=True)
Exhaust the iterator to detect every length mismatch.
Every value matters
zip_longest(a, b)
Define what a fill value means.
When does strict=True raise its error?
Validation happens when tuples are requested, rather than when the object is created. The iterator is lazy: it waits for a loop or list conversion to consume it.
keys = ["A", "B", "C"]values = [1, 2]pairs = zip(keys, values, strict=True)print(type(pairs).__name__)seen = []try: for pair in pairs: seen.append(pair)except ValueError as error: print(type(error).__name__ + ":", error)print(seen)$ python3 strict.pyzipValueError: zip() argument 2 is shorter than argument 1[('A', 1), ('B', 2)]The first two pairs were already appended to seen before the error. The message numbers arguments in call order: the second input is shorter than the first. Swapping the inputs changes that diagnostic.
A loop that exits with break after the first tuple may therefore never discover the mismatch. strict=True detects a problem; it doesn't undo earlier prints, appends or writes.
Step 1 of 3
Before the loop
- state
- ready
- seen[]
Creating zip hasn't checked any items yet.
Read all steps
1. Before the loop
- state
- ready
- seen[]
Creating zip hasn't checked any items yet.
2. After two pairs
- state
- iterating
- seen[('A', 1), ('B', 2)]
Both appends happened before the next request.
3. After ValueError
- state
- error
- seen[('A', 1), ('B', 2)]
The exception doesn't remove pairs already appended.
How do you validate before changing the result?
For finite inputs that fit in memory, materialize all the strict pairs before changing your destination. If conversion fails, the following statement doesn't run.
def collect_pairs(keys, values, saved): pairs = list(zip(keys, values, strict=True)) saved.extend(pairs)
saved = []try: collect_pairs(["A", "B", "C"], [1, 2], saved)except ValueError: print("ValueError")print(saved)collect_pairs(["A", "B"], [1, 2], saved)print(saved)$ python3 validate.pyValueError[][('A', 1), ('B', 2)]This function leaves saved unchanged when the lengths differ. It does store every intermediate pair, which costs memory. For ordinary lists, checking their lengths before the loop avoids that copy, provided the lists don't change between the check and iteration.
For generators or files, consumption itself may have effects. Building the list doesn't rewind inputs or make subsequent processing transactional. Full validation never finishes on an infinite stream.
How does zip_longest keep every value?
itertools.zip_longest() continues until the longest input is exhausted. It fills missing positions with None, or with your chosen fillvalue.
from itertools import zip_longest
keys = ["A", "B", "C"]values = [1, 2]print(list(zip_longest(keys, values)))print(list(zip_longest(keys, values, fillvalue=0)))$ python3 longest.py[('A', 1), ('B', 2), ('C', None)][('A', 1), ('B', 2), ('C', 0)]The zero in the last pair is padding; it wasn't in values. Don't use zero when missing data must remain distinct from a numeric zero. If None is also valid input data, use a dedicated sentinel and compare it by identity.
Can you recover the remaining iterator items?
Don't rely on zip() to preserve every value it doesn't yield. It requests input values from left to right; a request may consume a value from the first input before discovering that the second has ended.
left = iter([10, 20])right = iter([1])print(list(zip(left, right)))print(list(left))print(list(right))$ python3 iterators.py[(10, 1)][][]The value 20 was requested from left, but no tuple could be produced with it. Both iterators are exhausted. This doesn't contradict the list example: lists can be traversed again, while these iterator objects advance on each read. A zip object itself is also consumed only once.
How do you turn rows into columns?
zip(*rows, strict=True) takes each row as a separate input. It groups characters at the same position, so each tuple becomes a column read from top to bottom.
rows = ["cat", "owl"]print(list(zip(*rows, strict=True)))print(list(zip([], [], strict=True)))print(list(zip(strict=True)))try: print(list(zip("cat", "ox", strict=True)))except ValueError as error: print(type(error).__name__ + ":", error)$ python3 grid.py[('c', 'o'), ('a', 'w'), ('t', 'l')][][]ValueError: zip() argument 2 is shorter than argument 1The asterisk unpacks the list of rows into arguments. Without it, the list would be one input and each output tuple would contain a whole row. Here, both strings contain three characters, so three columns are produced. Equally empty rows produce an empty result, as does a call with no inputs.
The exercise expects strings for its columns, whereas zip() produces tuples. It also asks you to preserve spaces, handle an empty grid and leave the input rows unchanged. Make that adaptation yourself; indexed loops work too.
transpose_rows
Transpose a text grid: handle rectangular shapes, columns of spaces and empty inputs while keeping the original rows unchanged.
If your rows unexpectedly change together, read about copying nested lists. To order the records after pairing them, see sorting by multiple keys.
Frequently asked questions
Is strict=True available in Python 3.9?
No. It was added in Python 3.10. For lists on 3.9, check their lengths before iterating; iterators need another validation strategy.
Does strict=True check the values?
No. Equally long lists can still contain incompatible or incorrectly ordered data. The check concerns their simultaneous exhaustion.
Can zip take a single input?
Yes. Each output item is a one-element tuple. With no inputs, the result is empty, including with strict=True.
Can zip_longest work with infinite streams?
Yes, but it keeps going while any input can provide values. Bound the iteration explicitly if you need a finite result.
Sources
