Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python zip

Learn Python's zip function: parallel iteration, shortest-input behavior, strict length checking, multiple iterables, and unzipping paired data.

Levels: Beginner ~12 min read

Basic

Python's zip function combines values from multiple iterables position by position. It returns an iterator that produces tuples, one tuple for each position that all required inputs can supply.

Pair values side by side

The simplest use gives ziptwo or more iterables. The first values are grouped together, then the second values, and so on.

Interactive Runnable example
Edit

Ready to run.

Each result is a tuple containing the items from the same position. Because zip returns an iterator, you can consume it with a for loop or turn it into a list when you need the collected tuples.

Zip stops at the shortest input

By default, zip stops as soon as any input iterable is exhausted. Extra items in longer inputs are not included in the result.

Interactive Runnable example
Edit

Ready to run.

Require equal lengths with strict=True

Use strict=True when every input is expected to have the same length. The tuples produced are the same as ordinary zip, but a length mismatch raises ValueError instead of silently dropping the remaining items.

Interactive Runnable example
Edit

Ready to run.

The strict=True option was added in Python 3.10. It is especially useful when equal lengths are part of the meaning of the data rather than an accidental property.

Zip can combine more than two iterables

There is no special two-input limit. zip accepts zero or more iterables. With three inputs, each produced tuple has three items; with four inputs, each tuple has four items.

Interactive Runnable example
Edit

Ready to run.

A single iterable and no iterables

With one iterable, zip produces one-item tuples. With no arguments, it produces an empty iterator.

Interactive Runnable example
Edit

Ready to run.

Unzip paired data

Because zip creates tuples, the same function can help separate paired data. Put * before the zipped iterable when passing its tuples as separate positional arguments to zip again.

Interactive Runnable example
Edit

Ready to run.

Zip preserves input evaluation order

Python evaluates the iterables passed to zip from left to right. The resulting zip iterator then advances the input iterators together as it produces each tuple.

Interactive Runnable example
Edit

Ready to run.

When to use zip

Use zip when values from separate iterables represent the same positions and should be processed together. If you need numbered positions rather than another data sequence, enumerate is the dedicated built-in for that job and is already taught with loops.

Was this page helpful?

Mark this learning resource complete to track your learning progress.