The zip function in Python accepts iterable items and joins all of them into a single tuple. You can pass lists, sets, tuples or dictionaries through the zip() function. This results in a zip object that stores pairs of iterables.

In this article, we are going to break down the basics of the Python zip() function. We’ll also be discussing how to iterate over zip and how to unzip a zipped object.

Note: We use the term iterate in Python to describe when a program is running through a list.

What does zip do in Python?

Let’s say that we have a pair of lists, including first names and last names in this order:

list1 = ['Joey', 'Chandler', 'Bruce']
list2 = ['Tribbiani', 'Bing', 'Wayne']

Now we want them to be in this order as shown below-

[('Joey', 'Tribbiani'), ('Chandler', 'Bing'), ('Bruce', 'Wayne')]

We can achieve this with the zip function, which is a built-in python function. The zip function in Python works exactly in the same manner as a physical zipper. It brings two or more things together. It helps bring elements of the same index from multiple iterable objects together as elements of the same tuples. The iterator will stop once the shortest input iterable has been finished.

How to use zip in Python?

Let’s see how to use zip in python programming/code with various python examples-

How to zip two lists in python?

Based on the above given example, we can use the zip() function in this order:

list1 = ['Joey', 'Chandler', 'Bruce']
list2 = ['Tribbiani', 'Bing', 'Wayne']
full_names = list(zip(list1, list2))
print(full_names)

As a result, the Output is:
[('Joey', 'Tribbiani'), ('Chandler', 'Bing'), ('Bruce', 'Wayne')]

Note: The zip function returns an iterator. Thus, we need to use the list function that will use the zip object to create a list.

Using zip function with three lists

For example, if we add another list about ages with the above example. So, the zip function can take up any number of iterables.

list1 = ['Joey', 'Chandler', 'Bruce']
list2 = ['Tribbiani', 'Bing', 'Wayne']
age = [25, 35, 45]
Id = list(zip(list1, list2, age))
print(Id)

As a result, the Output is:
[('Joey', 'Tribbiani', 25), ('Chandler', 'Bing', 35), ('Bruce', 'Wayne', 45)]

Note: The Id object contains tuples with n elements.

Using lists or iterables with unequal lengths

Meanwhile, if we pass lists of unequal lengths, the iterator returned by the zip function will stop once the shortest input list has been finished. To sum it up, the list of tuples will only contain the elements from the indexes that are present in all the lists passed into the zip function. Thus, the remaining elements in the longer iterables or lists are ignored.

list1 = ['Joey', 'Chandler', 'Bruce', 'Leo']
list2 = ['Tribbiani', 'Bing', 'Wayne']
Id = list(zip(list1, list2))
print(Id)

As a result, the Output is:
[('Joey', 'Tribbiani'), ('Chandler', 'Bing'), ('Bruce', 'Wayne')]

Parallel iteration of lists or iterables

Since the zip() function returns an iterator, we can use this zip object in a for loop. Therefore, with each iteration of this iterator a tuple is returned, we can unpack the elements of this tuple within the for loop:

first_names = ['Joey', 'Chandler', 'Bruce']
last_names = ['Tribbiani', 'Bing', 'Wayne']

for first, last in zip(first_names, last_names):
    print(first, last)

As a result, the Output is:
Joey Tribbiani
Chandler Bing
Bruce Wayne

How To Unzip in Python?

We have the following given examples:

first_and_last_names = [('Joey', 'Tribbiani'), ('Chandler', 'Bing'), ('Bruce', 'Wayne')]

We want to separate the elements in these tuples into two separate lists. Since that is the opposite of zipping, it would be unzipping. To unzip in Python 3, we follow these steps:

first_names, last_names = zip(*first_and_last_names)
first_names = list(first_names)
last_names = list(last_names)

print(first_names)
print(last_names)

As a result, the Output will be:
['Joey', 'Chandler', 'Bruce'] ['Tribbiani', 'Bing', 'Wayne']

Note: The unpacking operator(*) will unpack the first_and_last_names list of tuples into its tuples. These tuples will then be passed to the zip function, taking these separate iterable objects and combines their same-indexed elements into two separate tuples. Further, through tuple unpacking the separated tuples assigned to the first_names and last_names variables. Subsequently, we use the list function to convert these tuples into lists.

Conclusion & Practical Application

In conclusion, we looked at how the zip() function works in python. We discussed how the zip function operates in different conditions, such as with one iterable, with iterables that have unequal lengths, how we can use the zip function to iterate over multiple iterable objects in parallel. Moreover, we learned how to use the unpacking operator(*) to unzip objects in python.

You can also learn about Python List Comprehension if else

Like this article? Follow us on Facebook and LinkedIn.