Python Glossary Русский

all Python 3Lists (list)term

zip() with lists

iteration

zip() joins the items of several iterables into tuples. It stops at the shortest one.

Syntax

zip(lst1, lst2, ...)  |  zip(*matrix)

Truncation to the shortest input happens silently — when the lengths must match, pass strict=True (Python 3.10+) so a mismatch raises ValueError instead of quietly dropping items. The result is a lazy, single-use iterator: after one list(zip(...)) or one loop it is exhausted and cannot be traversed again. If you need padding to the longest input rather than truncation, reach for itertools.zip_longest().

Examples

a = [1, 2, 3]
b = ['a', 'b', 'c']
print(list(zip(a, b)))  # → [(1, 'a'), (2, 'b'), (3, 'c')]
for x, y in zip(a, b):
    print(x, y)  # → 1 a / 2 b / 3 c
print(list(zip([1, 2, 3], [4, 5])))  # → [(1, 4), (2, 5)] — по короткому
print(dict(zip(['a', 'b', 'c'], [1, 2, 3])))  # → {'a': 1, 'b': 2, 'c': 3}
# Транспонирование: zip(*matrix)
matrix = [[1, 2, 3], [4, 5, 6]]
print(list(zip(*matrix)))  # → [(1, 4), (2, 5), (3, 6)]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy