How it works
zip produces tuples containing one item from each input iterable. By default it stops when the shortest input ends. It is lazy: constructing it does not immediately consume all the inputs.
Python 3.10 added strict=True. It raises ValueError when iteration discovers that the lengths differ. This checks a relationship between inputs, not the validity of each individual value.
Choices and common mistakes
Use strict=True when every item must have a partner. The mismatch is detected during consumption, so earlier loop iterations may already have run.
zip is not a database join: it matches by position, not by a shared key. Use a dictionary lookup when records need matching by identifier.
Try it yourself
Pair three product names with three prices. Remove a price and compare ordinary zip with strict=True. Explain which behaviour would be acceptable for your report.
Syntax
for name, score in zip(names, scores, strict=True):
print(name, score)Examples
Pair names and marks
Print a mark beside its corresponding student.
names = ["John", "James"]
marks = [72, 85]
for name, mark in zip(names, marks, strict=True):
print(name, mark)- Each iteration takes one name and one mark, then unpacks the tuple into name and mark.
- Both inputs have equal length, so strict iteration completes normally. This does not prove the two lists were originally sorted the same way.
- Use positional pairing only when the producer guarantees the same ordering.
- Expected output: John 72 James 85
Detect missing values
Reject unequal lengths instead of silently discarding data.
names = ["John", "James"]
marks = [72]
try:
pairs = list(zip(names, marks, strict=True))
except ValueError:
print("Each name needs a mark")- list forces zip to consume its inputs. The second name has no mark, which triggers ValueError.
- The assignment to pairs never completes. The handler gives a stable explanation instead of relying on interpreter-specific error wording.
- Validate the complete pairing before performing irreversible operations such as sending messages.
- Expected output: Each name needs a mark
Transpose a small table
Turn two equal-length rows into columns.
rows = [(1, 2, 3), (4, 5, 6)]
columns = list(zip(*rows, strict=True))
print(columns)- The star expands rows into separate arguments. zip then takes the first value of each row, followed by the second and third.
- list collects the resulting column tuples. strict=True catches ragged rows rather than truncating the wider row.
- This is useful for small rectangular tables already held in memory; the result is tuples, not a numeric matrix type.
- Expected output: [(1, 4), (2, 5), (3, 6)]
Best practices
- Use strict=True when every item must have a partner. The mismatch is detected during consumption, so earlier loop iterations may already have run.
- zip is not a database join: it matches by position, not by a shared key. Use a dictionary lookup when records need matching by identifier.
Runtime & versions
Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Python language reference