Python zip() function for Mapping (Tutorial)

Python Zip() Function Tutorial

This is a detailed tutorial of 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 keeping 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.


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 article that is 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 the scores of the same students.

Python Zip() Function Example For Zipping Iterables

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

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.

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 zipping and unzipping the 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.

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.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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