Chapters
Python114 chapters

ReferenceBuilt-in functions

zip()

Walks several sequences together, pairing up their items.

Signature

zip(*iterables, strict=False)

Parameters

NameMeans
*iterablestwo or more sequences
strictraise when the lengths differ

Returns

An iterator of tuples, stopping at the shortest input.

Example

Python
print(list(zip([1, 2], "ab")))
print(list(zip([1, 2, 3], [10, 20])))
print(dict(zip("ab", [1, 2])))

Output

[(1, 'a'), (2, 'b')]
[(1, 10), (2, 20)]
{'a': 1, 'b': 2}

See also