There is a plethora of in-built functions in Python that allow developers to create iterables to loop over a set of elements, each with its unique capabilities. One such useful built-in function is the zip method in Python. 

Before moving ahead, it’s important to understand what iterables are. In Python, when you use the word iterables, it refers to an object which has the capability to let us iterate over them, and return the elements. They can be tuples, lists, sets, dictionaries, etc.

The Zip() method in Python takes one or more iterables as input parameters or arguments and merges each of them element-wise to create a single iterable. The return value is a zip object, which is also an iterator and consists of tuples, which results from the merger between the elements of the input iterators. This means that the first elements of all the input iterators are zipped together to create a tuple which becomes the first element of the output zip object.  The same thing happens with the second elements of all the input iterators, and so on.

In simpler words, the zip method maps the same index of multiple input iterables and converts them into a tuple. The final zip objects contain all the mapped tuples in the same order. The general syntax of zip() method in Python is - 

zip(iterator_1, iterator_2, iterator_3, ...)

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

As discussed, the parameter values can be any valid iterable in Python. Also, if the lengths of the input iterables are different, then the input iterable which has the least size will be the length of the final zipped object.

Return Values of Zip in Python

The zip method in Python returns an iterator zipped object which contains tuples. The following are the scenarios based on the number of iterable that you pass as input - 

  1. If no input iterables are passed, then the zip() method will return an empty object or iterator.
  2. If you pass only one iterable, then it will return a zipped iterator object of tuples that each have only single elements.
  3. If you pass more than one iterable as input, then the zip() method will return tuples whose elements will be from all the input iterables. However, if the lengths of the input iterables differ, then the size of the returned iterator will be equal to the size of the smallest input iterable.

Let’s check all the possible scenarios with the help of some examples.

Example 1. Passing No Arguments

Look at this program where there is no passed input iterable to the zip method.

zippedObject = zip()

print("The returned values is - ", zippedObject)

print("Converting zipped object into a list - ", list(zippedObject))

Here, you have not passed any iterable as an argument to the zip method. Let’s check the output.

ZipinPython_Ex1.

You can see that the returned value is a zipped object. And when once it converts the zipped object into a list and prints it, it returns with an empty list.

Example 2. Passing One Argument

What happens if you pass only one iterable as an argument to a zip function? It will return a zipped object which will contain tuples, each having only one element of the input iterable. Let’s understand this with the help of an example.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe"]

zippedObject = zip(names)

print("The returned value is - ", zippedObject)

print("Converting zipped object into a list - ", list(zippedObject))

Here, you have a list of names and have passed this as an argument to the zip method in Python. Let’s check the output.

ZipinPython_Ex2

You can see that it returns a zipped object and when you convert it into a list and print it, it contains tuples, each having only one element.

Example 3. Passing Two Arguments

Let’s see what happens if you pass two iterables as arguments to the zip method. Consider the program below.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe"]

gender = ["Male", "Female", "Male", "Female", "Male", "Female"]

zippedObject = zip(names, gender)

print("The returned value is - ", zippedObject)

print("Converting zipped object into a list - ", list(zippedObject))

Here, this example has passed two lists to the zip method. Let’s check the output.

ZipinPython_Ex3

You can see that the returned zipped object when converted into a list has mapped tuples with elements from both the iterables.

Free Course: Programming with Python

Learn the Basics of Programming with PythonEnroll Now
Free Course: Programming with Python

Example 4. Passing n Arguments

If you pass over two arguments, irrespective of the fact whether they are of the same type or different types, it will map all of them together, to create tuples that are stored inside a zipped object and returned. Let’s check it out with the help of an example.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe"]

gender = ["Male", "Female", "Male", "Female", "Male", "Female"]

age = (35, 36, 38, 34, 33, 37)

zippedObject = zip(names, gender, age)

print("The returned value is - ", zippedObject)

print("Converting zipped object into a list - ", list(zippedObject))

Let’s verify the output of the above program.

ZipinPython_Ex4

You can see that all corresponding elements of all the input iterables irrespective of the types have been mapped into tuples and zipped into an object.

Example 5. Passing Arguments of Variable Lengths

What happens if the iterables that you pass as arguments have variable lengths? The zip method will return a zipped object whose length will be equal to the length of the smallest input iterable. Let’s check it out with the help of an example.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe", "Joanna", "Kate", "Lyndsey"]

gender = ["Male", "Female", "Male", "Female", "Male", "Female", "Female"]

age = (35, 36, 38, 34)

zippedObject = zip(names, gender, age)

print("The returned value is - ", zippedObject)

print("Converting zipped object into a list - ", list(zippedObject))

In the above program, the sizes of the names, gender, and age iterables are 9, 7, and 4 respectively. Let’s check out the size of the returned zipped object.

ZipinPython_Ex5

You can see that the size of the zipped object is 4, which is equal to the size of the smallest input iterable, which was the age set.

Example 6. Traversing Iterables in Parallel

You can use a for loop along with a zipped object to traverse over one iterables in parallel. For that, you can unpack the zipped object to get tuples in the for-loop header itself. Let’s try to traverse three lists in parallel.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe"]

gender = ["Male", "Female", "Male", "Female", "Male", "Female"]

age = (35, 36, 38, 34, 33, 37)

for n, g, a in zip(names, gender, age):

   print(f"{n} is a {g} and has an age of {a} years")

In the above program, you saw 3 lists that were passed as input iterables to the zip method directly in the loop's header. And you used the for loop, to traverse the tuples of the zipped object. In this way, you can traverse through the corresponding elements of all the input iterables parallelly. 

ZipinPython_Ex6

Let’s try to traverse two dictionaries simultaneously. Please note that dictionaries in Python are ordered, which means they keep the position of elements in which they were inserted. Hence, it can be used as input iterables to the zip method safely.

The output of the zip method over dictionaries as iterables will be a zipped object which will contain tuples. Each tuple will further contain tuples of key-value pairs of each of the corresponding iterables passed as input. 

Confusing, right? Understand this better with the help of a program.

names_and_gender = {"Chandler":"Male", "Monica":"Female", "Ross":"Male", "Rachel":"Female", "Joey":"Male", "Phoebe":"Female"}

names_and_age = {"Chandler":35, "Monica":36, "Ross":38, "Rachel":34, "Joey":33, "Phoebe":37}

for (k1, v1), (k2, v2) in zip(names_and_gender.items(), names_and_age.items()):

   print(f"{k1} is a {v1} and has an age of {v2} years")

In the above program, you have two dictionaries called names_and_gender and names_and_age respectively. You have used the for loop to get the key-value tuples of both the input iterables from the zipped object and printed them.

ZipinPython_Ex6_1

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Example 7. Unzipping a zipped object

You can unzip a zipped object by passing it back into the zip method, but with the unpacking operator (*). There is no unzip() method as such. Let’s see how to get the iterables back, after zipping them with the help of the program below.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe"]

gender = ["Male", "Female", "Male", "Female", "Male", "Female"]

zippedObject = zip(names, gender)

zippedList = list(zippedObject)

print("Converting zipped object into a list - ", zippedList)

unzipped_names, unzipped_gender = zip(*zippedList)

print("The names after unzipping are - ", unzipped_names)

print("The genders after unzipping are - ", unzipped_gender)

In the above program, after you have passed the zipped object back into the zip method with the unpacking operator, you will get them back as tuples. Check out the output below.

ZipinPython_Ex7

Applications of Zip in Python

The zip method in Python is quite useful. Some of the uses are - 

  1. It can be used to traverse across iterables parallelly and compare them.
  2. It can be used to sort a list of items based on elements in their corresponding positions in the other list.
  3. It can be used to perform operations in pairs.

There are several other uses as well. For example, you can use the zip method to map the marks of a student along with their names. Check out the code below.

names = ["Chandler", "Monica", "Ross", "Rachel", "Joey", "Phoebe"]

marks = [97, 32, 49, 60, 99, 78]

for name, mark in zip(names, marks):

   print(f"{name} has received a total of {mark} marks")

ApplicationsofZip 

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Wrapping Up!

To conclude, in this comprehensive guide, you saw how you can zip one or more iterables, and map their corresponding elements to tuples and return them as a zipped object. This article also discussed the syntax of the zip in Python along with the different types of return values. Finally, it explored a bunch of examples relating to different scenarios where you can use the zip method in Python and completed the tutorial, with a few real-life use cases of this method.

We hope that this comprehensive tutorial will help you to get hands-on experience with zip in Python. Have any questions for us? Leave them in the comments section of this article, and our experts will back to you on the same soon!

Happy Learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.