Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

zip

zip() iterates over multiple iterables in parallel and yields tuples of corresponding elements. It is lazy and can optionally require equal input lengths with strict=True.

Since
3.10

How it works

Call zip with one or more iterables to obtain a lazy iterator whose tuples contain corresponding elements from each input. Iteration advances the inputs in parallel; by default, it ends when the shortest input ends, while strict=True detects length mismatches.

Syntax

zip(*iterables, strict=False)

Parameters

NameTypeDescription
*iterablesiterableThe iterables whose corresponding elements should be grouped into output tuples.
strictboolIf true, require all iterables to be exhausted together; otherwise a length mismatch raises an exception.

Returns

A lazy iterator of tuples; each tuple contains one element from each input iterable.

Key points

  • zip combines corresponding positions from multiple iterables into tuples while traversing them together.
  • Each output tuple aligns elements by index across all supplied iterables.
  • It can transpose rectangular data conceptually, such as converting row-oriented records into columns.
  • The result defers work until consumption, making it suitable for loops and other iterator-based pipelines.
  • Use strict=True when mismatched lengths indicate invalid or unexpected input.

Common mistakes

  • Unequal input lengths are silently truncated unless strict=True is used; enable strict mode when equal lengths are required.
  • Passing only one iterable does not yield its values directly; each value is wrapped in a one-item tuple.
  • Calling zip() without inputs produces no pairs.

Example: Pair parallel iterables safely

names = ["Ada", "Linus", "Guido"]
roles = ["engineer", "designer", "author"]

for name, role in zip(names, roles, strict=True):
    print(f"{name}: {role}")

Pairs corresponding elements and raises an error if the inputs have different lengths.

When to use it

Parallel iteration

Associate records with corresponding labels, IDs, or configuration values while iterating in lockstep.

Transpose tabular data

Convert row-oriented sequences into column-oriented groups or reconstruct rows from columns.

Validate aligned inputs

Fail fast when parallel inputs must contain the same number of elements.

Interview questions

QWhat does zip() return and when does it stop?

zip() returns a lazy iterator of tuples. The i-th tuple contains the i-th element from every argument iterable, and iteration stops when the shortest input is exhausted by default.

Follow-up: What changes with strict=True? It validates that the iterables finish together and raises an exception when their lengths differ.

QWhy would you use zip(..., strict=True)?

strict=True asks zip() to verify that all input iterables have equal lengths. A mismatch raises an exception instead of silently discarding unmatched trailing elements.

Follow-up: When is this preferable? Use it when parallel data represents corresponding records and truncation would hide a bug.

QWhat does it mean that zip() is lazy?

zip() is lazy, so it does not process elements until the result is iterated, such as in a for loop or after conversion to list. This allows parallel processing without immediately materializing all tuples.

Follow-up: How can you materialize the result? Pass the iterator to list(), which consumes it and stores the resulting tuples.

  • iter()
  • list()
  • tuple()
  • map()

Source: Built-in functions. Checked 2026-10-15.