Python zip() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, zip(a, b, ...) returns an iterator of tuples that combine the first items of each iterable, then the second items, and so on: zip([1, 2], ["a", "b"]) yields (1, 'a') and (2, 'b').

It stops at the shortest input and silently ignores the rest. Since Python 3.10, strict=True raises ValueError when the lengths differ, and itertools.zip_longest pads the shorter inputs instead. zip(*pairs) does the reverse and “unzips” a list of pairs.

zip is a built-in type (a class) that you call like a function: each call creates a new zip object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Usually unpacked in a for loop.

python
for name, age in zip(names, ages):
    ...
pairs = list(zip(a, b, c))
CallWhat it does
list(zip([1, 2], "ab"))Returns [(1, 'a'), (2, 'b')].
dict(zip(keys, values))Builds a dict from two lists.
zip(*matrix)Transposes rows into columns.
zip(a, b, strict=True)Raises if lengths differ (Python 3.10+).

Printing a zip object shows only its type and address; wrap it in list() to see the tuples.

Library declarations
python
zip(*iterables, strict=False)
# class zip: returns an iterator of tuples; strict added in Python 3.10

Parameters

ParameterDescription
*iterablesZero or more iterables to combine.
strictOptional, keyword-only, Python 3.10+. True raises ValueError if the iterables have different lengths.

Return Value

Return ValueDescription
zipAn iterator of tuples, one item from each iterable per tuple.

Python Examples

Example 1 — Pair Names with Ages

Loop over two lists together and turn the pairs into a list and a dict.

python
names = ["Ana", "Ben", "Cy"]
ages = [31, 25, 40]

for name, age in zip(names, ages):
    print(name, age)

print(list(zip(names, ages)))
print(dict(zip(names, ages)))

How It Works

  1. Parallel loop. Each step takes one name and the age at the same position.
  2. Tuples. list(zip(...)) shows the pairs zip produces.
  3. Instant dict. dict() accepts the pairs and uses names as keys.
  4. Fresh zip each time. Each line calls zip() again, because an iterator can be used only once.

Example 2 — Shortest Input vs zip_longest

See items dropped by zip() and kept by itertools.zip_longest.

python
from itertools import zip_longest

letters = ["a", "b", "c", "d"]
numbers = [1, 2]

print(list(zip(letters, numbers)))
print(list(zip_longest(letters, numbers, fillvalue=0)))
print(list(zip()))

How It Works

  1. Stops at the shortest. numbers has two items, so c and d are dropped without any error.
  2. Pad instead. zip_longest continues to the longest input and fills gaps with fillvalue.
  3. Default fill. Without fillvalue, the gaps would be None.
  4. No inputs. zip() with no arguments yields nothing.

Example 3 — Unzip, Transpose, and strict=True

Split pairs back into two tuples, flip rows and columns, and catch mismatched lengths on Python 3.10+.

python
import sys

pairs = [("x", 1), ("y", 2), ("z", 3)]
keys, values = zip(*pairs)
print(keys, values)

matrix = [[1, 2, 3], [4, 5, 6]]
print([list(col) for col in zip(*matrix)])

if sys.version_info >= (3, 10):
    try:
        list(zip([1, 2, 3], ["a", "b"], strict=True))
    except ValueError:
        print("strict=True: lengths differ")

How It Works

  1. Unzip with *. zip(*pairs) passes each pair as a separate argument, so the first items group together and the second items group together.
  2. Transpose. The same trick on a matrix turns two rows of three into three rows of two.
  3. Strict mode. With strict=True, zip raises ValueError as soon as one input runs out early.
  4. Version guard. The check runs only on Python 3.10+, where strict exists.

Frequently Asked Questions

It combines several iterables into tuples, position by position: zip([1, 2], ["a", "b"]) yields (1, 'a') and (2, 'b').
zip() stops at the shortest. Use itertools.zip_longest to keep going, or strict=True (Python 3.10+) to get an error.
Use a, b = zip(*pairs). Each result is a tuple.
dict(zip(keys, values)).
zip returns a lazy iterator, and its repr only shows the type and an address. Use list(zip(a, b)).

Next: __import__()

The function behind the import statement.

__import__() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful