How does zip() work in Python?

Key takeaways:

  • The zip() function pairs elements from multiple iterables, creating tuples of corresponding items at each position.

  • When input iterables have different lengths, zip() stops at the shortest iterable, preventing index errors.

  • You can reverse the zipping process using the * operator to separate the combined iterables back into their original form.

The zip() function in Python is used to combine multiple iterables (such as lists, tuples, etc.) element by element, creating pairs (or tuples) of corresponding elements from the given iterables.

The zip() method in Python returns a zip object by taking in iterables, like lists or strings, and aggregating them into tuples.

High level overview of zip() function
  • No parameter: When given no iterable as a parameter, zip() returns an empty iterator.

  • Single parameter: When given a single iterable argument, zip() returns an iterator of tuples with each tuple only having one element.

  • Multiple parameters: When given multiple iterables, zip() returns an iterator of tuples with each tuple having multiple elements.

serialNumbers = [1, 2, 3]
names = ['Sarah', 'John', 'Harry']
# No iterables are passed
result = zip()
print(result)
# With a single iterable
result = zip(serialNumbers)
print(result)
# With multiple iterables
result = zip(serialNumbers, names)
print(result)

The Python code demonstrates how the zip() function behaves when used with different numbers of iterables. First, an empty zip() call is made, which produces an empty iterator since no iterables are provided. Next, zip() is used with a single iterable (serialNumbers), but instead of pairing elements, it simply returns a zip object without any meaningful transformation. Finally, when two iterables (serialNumbers and names) are passed, zip() pairs corresponding elements from both lists, effectively creating tuples like (1, 'Sarah'), (2, 'John'), and (3, 'Harry').

Purpose

The purpose of this function is to map the elements of iterables to the same indexes in the tuples.

zip() function mapping
zip() function mapping

Let’s practically implement how to use zip() to pair elements from two lists and iterate through them.

serialNumbers = [1, 2, 3]
names = ['Sarah', 'John', 'Harry']
result = zip(serialNumbers, names)
print(result)
# Now, to get a clear picture of how zip()
# function is mapping both the iterables:
for a,b in zip(serialNumbers, names):
print(a,b)

The given Python code demonstrates how the zip() function pairs elements from two lists and iterates over them. Initially, zip(serialNumbers, names) creates an iterator that pairs corresponding elements from serialNumbers ([1, 2, 3]) and names (['Sarah', 'John', 'Harry']).

Reading the result

To obtain the output in a readable format, you can use tuple() to view the result in the form of tuples. You may also use list() or set() functions, as seen below:

serialNumbers = [1, 2, 3]
names = ['Sarah', 'John', 'Harry']
result = zip(serialNumbers, names)
# Use tuple() function to read the result
print(tuple(result))
# Converting itertor to list
resultList = list(result)
print(resultList)
# Converting to set
resultSet = set(result)
print(resultSet)

This Python code demonstrates how the zip() function pairs elements from two lists and converts the resulting iterator into different data structures. The zip(serialNumbers, names) function pairs elements from serialNumbers ([1, 2, 3]) and names (['Sarah', 'John', 'Harry']), forming an iterator of tuples: [(1, 'Sarah'), (2, 'John'), (3, 'Harry')].

  • tuple(result): Converts the zip iterator into a tuple, preserving the element order.

  • list(result): Converts the iterator into a list, but since iterators are exhausted after one use, this will return an empty list if called after tuple(result).

  • set(result): Converts the iterator into a set, but again, since the iterator is exhausted, this will return an empty set if called after previous conversions.

What happens with input iterables of different lengths?

The iterator returned form zip() stops when one of the input iterable is exhausted.

serialNumbers = [1, 2, 3, 4, 5] # length = 5
names = ['Sarah', 'John', 'Harry'] # length = 3
for a,b in zip(serialNumbers, names):
print(a,b)

The Python code demonstrates how the zip() function pairs elements from two lists of different lengths. The serialNumbers list contains 5 elements ([1, 2, 3, 4, 5]), while the names list contains only 3 elements (['Sarah', 'John', 'Harry']). When zip() is applied, it pairs elements until the shortest iterable is exhausted. Therefore, only the first three elements are paired, and the remaining elements (4 and 5 from serialNumbers) are ignored.

Learn the basics with our engaging course!

Start your coding journey with our “Learn Python” course, the perfect course for absolute beginners! Whether you’re exploring coding as a hobby or building a foundation for a tech career, this course is your gateway to mastering Python—the most beginner-friendly and in-demand programming language. With simple explanations, interactive exercises, and real-world examples, you’ll confidently write your first programs and understand Python essentials. Our step-by-step approach ensures you grasp core concepts while having fun along the way. Join now and start your Python journey today—no prior experience is required!

Conclusion

In conclusion, the zip() function in Python is a powerful and efficient tool for combining multiple iterables element by element, producing tuples of corresponding items. It is particularly useful when you need to pair data from different sources, such as combining two lists or iterating over multiple sequences simultaneously. While it stops at the shortest iterable when the input sequences have unequal lengths, it offers flexibility in “unzipping” as well, allowing you to separate combined iterables back into their original components. Overall, zip() is an essential function for working with parallel data in Python.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is z`ip()` in Python?

zip() in Python is a built-in function that creates an iterator of tuples, where the i-th tuple contains the i-th element from each of the input iterables.


What is the difference between `map()` and `zip()` in Python?

  • map() applies a function to each element of an iterable and returns an iterator of the results.

  • zip() combines elements from multiple iterables into tuples and returns an iterator of those tuples.


What are `zip()` and `unzip()` functions in Python?

  • zip(): This combines multiple iterables into an iterator of tuples, where each tuple contains elements from the corresponding positions of the input iterables.

  • unzip(): There’s no built-in unzip() function in Python.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved