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.
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.
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 passedresult = zip()print(result)# With a single iterableresult = zip(serialNumbers)print(result)# With multiple iterablesresult = 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')
.
The purpose of this function is to map the elements of iterables to the same indexes in the tuples.
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']
).
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 resultprint(tuple(result))# Converting itertor to listresultList = list(result)print(resultList)# Converting to setresultSet = 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.
The iterator returned form zip()
stops when one of the input iterable is exhausted.
serialNumbers = [1, 2, 3, 4, 5] # length = 5names = ['Sarah', 'John', 'Harry'] # length = 3for 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!
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.
Haven’t found what you were looking for? Contact Us
Free Resources