In this article we will explore how to use the Python zip() function.
Table of Contents
- Introduction
- Create iterator using zip() in Python
- Unzip a list of tuples using zip()
- Create a dictionary from two lists using zip()
- Conclusion
Introduction
In Python, when we work with multiple iterables (lists, tuples, or strings), we often need to iterate over multiple of them simultaneously.
Python zip() function is a built-in function that allows to “zip” multiple iterables together into a single iterable object of tuples, where each tuple contains the elements from the corresponding position in each of the input iterables.
Create iterator using zip() in Python
As discussed in the previous section, Python zip(*iterables) function takes n iterables as arguments and returns a single iterator of series of tuples.
Let’s take a look at a few examples!
Passing 2 iterables
Using zip() function with 2 iterables of equal length will generate an iterator with a sequence of tuples of length 2.
Consider the following example:
#Create 2 iterables
fruits = ['Apple', 'Banana', 'Orange']
prices = [1, 3, 5]
#Create an iterator
zipped = zip(fruits, prices)
#Access tuples generated from 2 lists
for t in zipped:
print(t)
and you should get:
('Apple', 1)
('Banana', 3)
('Orange', 5)
Alternatively, you can modify the for loop to unpack the tuples and print formatted strings:
#Unpack the tuples
for fruit, price in zipped:
#Print formatted string
print(f'The price of {fruit} is {price}')
and you should get:
The price of Apple is 1
The price of Banana is 3
The price of Orange is 5
Passing n iterables
Using zip() function with n iterables of equal length will generate an iterator with a sequence of tuples of length n.
Consider the following example:
#Create 3 iterables
fruits = ['Apple', 'Banana', 'Orange']
prices = [1, 3, 5]
colours = ['G', 'Y', 'O']
#Create an iterator
zipped = zip(fruits, prices, colours)
#Access tuples generated from 3 lists
for t in zipped:
print(t)
and you should get:
('Apple', 1, 'G')
('Banana', 3, 'Y')
('Orange', 5, 'O')
Alternatively, similar to the 2 iterables case, you can modify the for loop to unpack the tuples and print formatted strings:
#Unpack the tuples
for fruit, price, colour in zipped:
#Print formatted string
print(f'The price of {fruit} is {price} and it is {colour}')
and you should get:
The price of Apple is 1 and it is G
The price of Banana is 3 and it is Y
The price of Orange is 5 and it is O
Passing iterables of unequal length
Now, there could always be a case when one or more of the iterables are of different lengths.
What happens then?
In this case, the number of tuples in the iterator generated by the zip() function will be equal to the length of the shortest iterable.
Consider the following example:
#Create 2 iterables of unequal lengths
fruits = ['Apple', 'Banana', 'Orange']
prices = [1, 3]
#Create an iterator
zipped = zip(fruits, prices)
#Access tuples generated from 2 lists
for t in zipped:
print(t)
and you should get:
('Apple', 1)
('Banana', 3)
Notice that the ‘Orange‘ element from the fruits list isn’t printed out, it happens because the length of prices list is 2, whereas the length of fruits list is 3, with the last element in fruits being unmated with an element in prices.
Passing 1 iterable
Using zip() function with 1 iterable will generate an iterator with a sequence of 1 item tuples.
Consider the following example:
#Create 1 iterable
fruits = ['Apple', 'Banana', 'Orange']
#Create an iterator
zipped = zip(fruits)
#Access tuples generated from 1 list
for t in zipped:
print(t)
and you should get:
('Apple',)
('Banana',)
('Orange',)
As you can see, this may not be the best and most functional use of the zip() function.
Passing no iterables
Using zip() function with no iterables will generate an empty iterator.
Consider the following example:
#Create an iterator
zipped = zip()
#Access tuples
for t in zipped:
print(t)
And you should get no output since there are no tuples in an empty iterator.
Unzip a list of tuples using zip()
Just like we can zip multiple iterables into one iterator, we can use the zip() function to unzip a list of iterables into iterable tuples using the * operator.
Consider the following example:
#Create a zipped list
zipped_list = [('Apple', 1), ('Banana', 3), ('Orange', 5)]
#Unzip a list of iterables
fruits, prices = zip(*zipped_list)
#Print generated tuples
print(fruits)
print(prices)
and you should get:
('Apple', 'Banana', 'Orange')
(1, 3, 5)
Create a dictionary from two lists using zip()
Another useful functionality of the zip() function is that it allows to create a dictionary from multiple lists using dict() constructor with the iterator object.
Consider the following example:
#Create 2 iterables
fruits = ['Apple', 'Banana', 'Orange']
prices = [1, 3, 5]
#Create an iterator
zipped = zip(fruits, prices)
#Create a dictionary from the iterator
fruit_prices_dict = dict(zipped)
#Print the dictionary
print(fruit_prices_dict)
and you should get:
{'Apple': 1, 'Banana': 3, 'Orange': 5}
Conclusion
In this article we explored the Python zip() function.
Now that you know the basic functionality, you can practice using it with other iterable data structures for more complex use cases.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Functions tutorials.