How to use Python Zip – with example

  • Reading time:16 mins read

What is Python Zip?

Python zip is a built-in Python function that allows us to create an iterator of tuples by mapping together elements from iterable based on their index numbers. 

How to use the Python Zip function?

For example, we can use the Python zip function to create another zip object that will combine elements from the two lists. In this case, we will need to create the two lists that we intend to zip together, and then create a variable that will store our new zipped object.

We are going to call this object zipped and then to apply the zip function to the two lists that we just created we will need to use the word zip followed by a pair of parentheses and then pass both lists that we want to be zipped between the parentheses separated by a comma.

If we print out the variable zipped we will end up with a zip object in the console or terminal.

 

Python zip

Printing out the elements of a zipped object

In order to get the actual list values of the zip object, we need to convert the object to a list. Now the Python list method list() takes sequence types and converts them to lists.

 

 

We can see in the output that the zip function takes elements that fall in the same index from both lists and creates a tuple for each one of them. For instance ‘elephant’ and ‘4’ fall within the same index and therefore have been placed in the same tuple.

This is also the case for the rest of the elements in both iterables. So ‘tiger’ and ‘6’ will also be paired together in one tuple.

 

python zip

 

Now we should, however, note that the standard zip function does not work when we have lists or tuples with mismatched lengths i.e if one list is longer than the other.

Using zip function on lists

So for example we can add a fourth item to the list of animals so that it now contains: [‘elephant’, ‘tiger’, ‘lion’, ‘monkey’] and on the other hand maintain the qty list unchanged.

If we try to run the zip function on the two lists once again we would note that the function will ignore the elements that come after the length of the shorter list.

 

 

Now we can import zip_longer function from Iter tools which are also built into Python and use a fill value if we want to include all the elements in the long list. However, that is not covered here.

Return a set with Python Zip

We can also return a set instead of a list by simply replacing the list() method with the set() method instead. This will return a set of tuples instead of a list of tuples. 

 

Unpack Zip elements

If we wish to unpack the zip elements we will have to convert the list object into a list. Since there is no unzip function, we need to use the argument given to us.

We are going to create two variables to store the values that we are going to unpack in their respective lists as they were initially. We are going to equate the two variable names to the Python zip function that takes in the new_zip  variable storing the list of tuples zipped together. 

Note that this time since we are unpacking elements already zipped together we need to add a star just before the *new_zip variable that is storing the already zipped object. This is going to basically reverse the process and unzip the zipped object so that when we run the code below we should get the original lists that we began with.

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.