DEV Community

Cover image for Zip Function In Python
Vishesh Dvivedi
Vishesh Dvivedi

Posted on

Zip Function In Python

All About the Zip Function In<br>
Python

The zip function in python is a great function that is used to merge two
or more iterable items into one, and that too by index. It's a built-in
function of python language which can make your life easy when you have
data with different formats but need to join them somehow.

Before we start with the zip function, let's talk about the iterable
data types in python.

Iterable Data Types In Python

Iterable data types are basically those data types that can be iterated
i.e. a for-in-the-loop can be used to go through each element within
them. Some examples of iterable data types are lists, tuples, sets, etc.

lists_example = [1, 2, 3, 4, 5]
tuple_example = (1, 2, 3)
sets_example = {1, 2, 3}
Enter fullscreen mode Exit fullscreen mode

Any of these data types can be passed onto a for-in loop which will
iterate through each of their elements.

What does the zip() function has to do with iterable data types?

Assume a situation where you have to display data of students that study
in a school. You happen the have to have lists of data of students. But
the catch here is that all the names are in one list, and all their
student IDs are on one list, like this:

student_names = ["Alex", "Murphy", "Jinny", "George"]
student_ids = [700405, 700465, 700487, 700502]
Enter fullscreen mode Exit fullscreen mode

How will you print student's information together?

One approach can be to use for loop to print values by index, like this:

for i in range(len(student_names)):    
    print("ID: {0} name: {1}".format(student_ids[i], student_names[i])) 
Enter fullscreen mode Exit fullscreen mode

But what if you want the output in the form of a list contains tuples
for each student with its name and ID in it?

This is where the zip function comes into play !!!

You can achieve the above-mentioned results easily using the zip
function, list this:

final_result = list(zip(student_ids, student_names))
Enter fullscreen mode Exit fullscreen mode

And that's it! That's all you'll have to do.

The output of the zip function would look something like this:

[(700405, 'Alex'), (700465, 'Murphy'), (700487, 'Jinny'), (700502, 'George')]
Enter fullscreen mode Exit fullscreen mode

Isn't that neat !!!

How to reverse the effects of zip function?

Although there are various ways to undo the effects of the zip function,
the best approach will be to use the zip function and pass the list with
the * operator to unzip like this

list_to_unzip = [(700405, 'Alex'), (700465, 'Murphy'), (700487, 'Jinny'), (700502, 'George')]

unzipped = zip(*list_to_unzip)
Enter fullscreen mode Exit fullscreen mode

The value of unzipped will be like this:

[(700405, 700465, 700487, 700502), ('Alex', 'Murphy', 'Jinny', 'George')]
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope this article helped you to understand the working of the zip
function and would add up to your existing knowledge of python.

In case of any doubts, you can comment down all your queries and I will
try to solve them as soon as possible.

Also, don't forget to check out my YouTube
Channel
if you liked this article

With this, it's time for me to go, this is All About Python, signing off

Top comments (0)