# Python etc / zip

Published: 4 January 2022, 18:00.

zip function allows you to iterate over multiple iterators at the same time (I know you know it, just bear with me):

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


But what if the given iterators have a different length? Then the resulting iterator will have the shortest length:

list(zip([1, 2], [3, 4, 5]))
# [(1, 3), (2, 4)]


If you need all values, there is itertools.zip_longest:

from itertools import zip_longest
list(zip_longest([1, 2], [3, 4, 5], fillvalue=None))
# [(1, 3), (2, 4), (None, 5)]


But what if you want to ensure that both iterators have the same length? For that purpose, PEP-618 (landed in Python 3.10) introduced strict flag:

list(zip([1, 2], [3, 4, 5], strict=True))
# ValueError: zip() argument 2 is longer than argument 1