On this page
zip
Prev: vars | Up: __builtins__
Rel: all, any, enumerate, filter, iter
Ref: docs.python.org — zip
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import zip
Overview & Signature
zip(*iterables, strict=False)
The zip object yields n-length tuples, where n is the number of iterables passed as positional arguments to zip(). The i-th element in every tuple comes from the i-th iterable argument to zip(). This
Usage
names = ["Ada", "Grace"]
scores = [98, 100]
assert list(zip(names, scores)) == [("Ada", 98), ("Grace", 100)]
# Unzip with zip(*pairs)
a, b = zip(*[("Ada", 98), ("Grace", 100)])
assert a == ("Ada", "Grace") and b == (98, 100)
Gotchas
- Silently truncates to the shortest iterable by default: Pass
strict=True(Python 3.10+) to raiseValueErroron mismatched lengths, or useitertools.zip_longest()to pad shorter inputs.
REPL Inspect
help(zip)
Linked from
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/vars Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/all Linked here; shared tags: #builtins
- tuts/python3/__builtins__/any Linked here; shared tags: #builtins
- tuts/python3/__builtins__/enumerate Linked here; shared tags: #builtins
- tuts/python3/__builtins__/filter Linked here; shared tags: #builtins
- tuts/python3/__builtins__/iter Linked here; shared tags: #builtins
- tuts/python3/tutorials/core_fundamentals/control_flow links to this page
- tuts/c/builtins shared tags: #builtins