Python zip() Function – Usage with Examples

This is a detailed tutorial on the python zip() function. Learn to map similar values contained in different containers into a single container or iterable.

Python zip() function

The zip() function in python is used to map similar values that are currently contained in different containers into a single container or an iterable. In other words, if you have a similar collection of data that are contained in different variables, you can do a kind of data merging with mapping and hence keep the similar items that are currently in different containers together in the resultant container.

You’ll get a better insight into how this function works and what are its use cases after you’ll have a look at the example.

Syntax

The syntax of the zip() function is given below. It takes a variable number of parameters that are basically different tables that contain similar data that can be mapped together in the resultant iterable. So, this function returns an iterable that contains the data from provided iterable arguments mapped together.

The iterators can be a list, string, tuple, etc. The * in front of the passed argument iterators means that it can take variable arguments. We’ve already talked about this notation to accept multiple and variable arguments in a single function in one of the previous articles which are linked below for your reference.

The resutant_iterable will be a single iterable object that contains mapped values from all the iterables provided as arguments to the zip() function.

Zipping the Iterables

The following example illustrates the use of the zip() function to map three different iterables that contains the names, age, and scores of the same students.

name = ["Gurmeet Singh","Amandeep Singh","Simrandeep Kaur","Manmeet Singh"]
age = [21,12,10,9]
score = [99,80,85,75]

#Mapping name, age, and score into a single iterable
stats = zip(name,age,score)
print(stats)

#Iterating over the zip object
for one in stats:
    print(one[0] + " is aged " + str(one[1]) +" and scored " + str(one[2]) +".")

Python Zip() Function Example For Zipping Iterables

You can also first convert the zip object formed into a set and can then iterate or can index the required details easily. This is also illustrated in the example given below.

name = ["Gurmeet Singh","Amandeep Singh","Simrandeep Kaur","Manmeet Singh"]
age = [21,12,10,9]
score = [99,80,85,75]

#Mapping name, age, and score into a single iterable
stats = zip(name,age,score)
print(stats)

#Converting zip object to set
stats = set(stats)
print(stats)

Python Zip() Function Example Zipping And Converting To Set

This way, you can map any number of iterables into a single iterable container. Just make sure the data values in each of the iterable must be similar. In case any entry for a particular data record from any one of the iterable will be missing, the entire record will not be taken into consideration from any of the provided iterable.

Also, the order matters a lot. If the passed iterable arguments will contain data in a different order, it may result in the wrong mapping of the data.

Unzipping the Iterables

You can also use the zip() function for unzipping an iterable that already contained mapped data into separate iterables. Again for this, we’ll use the *args syntax.

name = ["Gurmeet Singh","Amandeep Singh","Simrandeep Kaur","Manmeet Singh"]
age = [21,12,10,9]
score = [99,80,85]

#Mapping name, age, and score into a single iterable
stats = zip(name,age,score)
print(stats)

#Converting zip object to set
stats = set(stats) #This is optional, zipped objects can also be directly unzipped
print(stats) #This is optional

name, age, score = "", "", ""
name, age, score = zip(*stats)
print(name)
print(age)
print(score)

Unzipping Python Mapped Iterables Example Using Zip() And *args

Use of zip() function For Iteration

Usually, rather than creating and storing the zip objects into variables, the zip() function is often used for mapped iterations whenever required. So it eliminates the need to zip and unzip things again and again for individual containers containing the atomic data for further processing in the python program.

The following example is making use of the zip() function for such an iteration.

name = ["Gurmeet Singh","Amandeep Singh","Simrandeep Kaur","Manmeet Singh"]
age = [21,12,10,9]
score = [99,80,85]

#Zipped Iteration
for one in zip(name, age, score):
    print(one)
    
#Another Type of Zipped Iteration
for name, age, score in zip(name, age, score):
    print(name + " is aged " + str(age) +" and scored " + str(score) +".")

Python Zipped Iteration Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *