iteration

zip()

Pairs elements from multiple iterables into tuples, stopping at the shortest. With strict=True, raises ValueError if lengths differ.

zip(*iterables, strict=False)

Parameters

Parameter Purpose
*iterables Two or more iterables to combine
strict Require equal-length inputs (Python 3.10+)

Examples

list(zip([1, 2], ['a', 'b']))

Returns [(1, 'a'), (2, 'b')]

list(zip([1, 2, 3], ['a', 'b']))

Returns [(1, 'a'), (2, 'b')] — extra dropped

dict(zip(['x', 'y'], [1, 2]))

Returns {'x': 1, 'y': 2}

list(zip(*[[1, 2], [3, 4]]))

Returns [(1, 3), (2, 4)] — matrix transpose

Gotcha

Silent truncation on unequal lengths is a common bug — use strict=True (Python 3.10+) when lengths must match.

Related built-ins

← All Python built-ins