Python Reference Library →

Python zip() Function – Combine Iterables Element-Wise

Let’s look at zip() function – combine iterables element-wise

  1. Notice

    zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))
Live state

Top-level scope

numbers→Object 1
letters→Object 2
Object 1 · rangerange(0, 3)Object 2 · list['a', 'b']
This step

zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.

Picture zip() function – combine iterables element-wise

zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.

Image brief

Alt text
A diagram for Python zip() Function – Combine Iterables Element-Wise. Instructions: numbers = range(3) letters = ["a", "b"] print(list(zip(numbers, letters))). Displayed result: [(0, 'a'), (1, 'b')]. The final example bindings are numbers --> object 1 (range); letters --> object 2 (list). object 1: range(0, 3); object 2: ['a', 'b']
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python zip() Function – Combine Iterables Element-Wise". Teaching focus: zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = range(3) letters = ["a", "b"] print(list(zip(numbers, letters))) | v DISPLAYED RESULT [(0, 'a'), (1, 'b')] FINAL BINDINGS numbers --> object 1 (range) letters --> object 2 (list) object 1: range(0, 3) object 2: ['a', 'b'] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python zip() Function – Combine Iterables Element-Wise. Instructions: numbers = range(3) letters = ["a", "b"] print(list(zip(numbers, letters))). Displayed result: [(0, 'a'), (1, 'b')]. The final example bindings are numbers --> object 1 (range); letters --> object 2 (list). object 1: range(0, 3); object 2: ['a', 'b']

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))
Live state

Top-level scope

numbers→Object 1
letters→Object 2
Object 1 · rangerange(0, 3)Object 2 · list['a', 'b']
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[(0, 'a'), (1, 'b')]

What do you expect this program to print?

What does this example display?

numbers = range(3)
letters = ["a", "b"]
print(list(zip(numbers, letters)))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[(0, 'a'), (1, 'b')]

Follow the printed values in order. The output is: [(0, 'a'), (1, 'b')]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.

zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.

More examples and reference

Python zip() Function

The zip() function in Python combines two or more iterables (like lists or tuples) element by element into pairs or groups. It’s extremely useful when you want to loop through multiple sequences at once.

Syntax

zip(iterable1, iterable2, ...)

Parameters:

  • iterable1, iterable2, ... – Two or more iterables (lists, tuples, strings, etc.)

Returns:

  • A zip object – which is an iterator of tuples, where the i-th tuple contains the i-th element from each iterable.

Example: Zipping Two Lists

names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 95]

zipped = zip(names, scores)
print(list(zipped))
[('Alice', 85), ('Bob', 90), ('Charlie', 95)]

Example: Zipping Three Iterables

a = [1, 2, 3]
b = ['a', 'b', 'c']
c = [True, False, True]

print(list(zip(a, b, c)))
[(1, 'a', True), (2, 'b', False), (3, 'c', True)]

What If Iterables Are of Unequal Length?

When iterables passed to zip() are not of the same length, it stops at the shortest one.

x = [1, 2]
y = ['a', 'b', 'c']

print(list(zip(x, y)))
[(1, 'a'), (2, 'b')]

Use Case: Creating a Dictionary

keys = ['id', 'name', 'age']
values = [101, 'Alice', 25]

person = dict(zip(keys, values))
print(person)
{'id': 101, 'name': 'Alice', 'age': 25}

Looping with zip()

for name, score in zip(names, scores):
    print(f"{name} scored {score}")
Alice scored 85
Bob scored 90
Charlie scored 95

Unzipping Using * Operator

You can reverse a zipped object using the unpacking operator *:

pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
a, b = zip(*pairs)
print(a)
print(b)
(1, 2, 3)
('a', 'b', 'c')

Common Mistakes

  • Forgetting to convert the zip object to a list when printing: print(zip(...)) will just show the object type.
  • Expecting zip() to handle different lengths – it doesn't pad, it truncates.

Interview Tip

zip() is frequently used in data pairing, matrix manipulation, and transforming data between structures. Be ready to combine or unzip data structures in questions.

Summary

  • zip() combines multiple iterables into tuples.
  • Stops when the shortest iterable is exhausted.
  • Great for pairing keys and values, parallel looping, and restructuring data.

Practice Problem

Given two lists of student names and their grades, print each student with their grade using zip().

students = ["Ravi", "Meena", "Arjun"]
grades = ["A", "B", "A+"]

for student, grade in zip(students, grades):
    print(f"{student} got grade {grade}")

What to remember

Connect what you noticed in the example with the idea below.

Remember

zip() produces tuples of corresponding items. By default it stops at the shortest input; strict=True can detect mismatched lengths.

Watch out for this idea

zip() always fills missing items with None.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →