zip() function creates an iterator that merges elements from multiple data sources. Python
zip() works with lists, tuples, sets, or dictionaries to create lists of tuples containing the combined data.
Python has a number of built-in functions that allow coders to loop through data. One of these functions is Python zip. The
zip() function creates an iterator that will merge elements from two or more data sources into one.
zip() function has a wide variety of applications. For example, if you want to create a set of dictionaries from two arrays, each of which holds an employee name and their employee number, we could do that using
In this tutorial, we are going to break down the basics of Python
zip(), discuss how it can be used in Python 2 and Python 3, and explore where it can be useful.
Python Iteration Refresher
In Python, we use the term
iterate to describe when a program is running through a list. For example, if you have a
for loop that prints out the name of every branch a company operates, we would say that our program is
iterating through the list of names.
iterable, on the other hand, is an object that can return its member items individually. So, arrays are iterables because you can print out each item individually by using a
for loop. Now that we have reminded ourselves of the basics of iteration in Python, we can go on to explore the Python
zip() function takes an iterable—such as a list, tuple, set, or dictionary—as an argument. The function will then generate a list of tuples that contain elements from each iterable you have passed into the function.
Let’s say you have a list of employee names and numbers, and you want to merge them all into an array of tuples. We can use the
zip() function to achieve this goal. Here is an example program that will merge this data:
employee_numbers = [2, 9, 18, 28] employee_names = ["Candice", "Ava", "Andrew", "Lucas"] zipped_values = zip(employee_names, employee_numbers) zipped_list = list(zipped_values) print(zipped_list)
Our zip function returns the following:
[('Candice', 2), ('Ava', 9), ('Andrew', 18), ('Lucas', 28)]
Our program has created an array of tuple items, each of which contains the name of an employee, and their employee number.
There is a lot going on in our code, so let’s break it down. On the first two lines, we declare variables that store our employee numbers and employee names. On the next line, we perform a
zip() function that merges those two lists together and creates a new array of tuples.
On the next line, we convert our zip item—which has the data type
zip—into a list, so that we can print it out and use it as a list. Then, on the final line, we print out our zipped list.
We can tell that our
zipped_values is a
zip() item by adding the following code to our above program:
Our code returns the following zipped class:
In the above example, we zipped two items together. However, if we had more that we wanted to zip, we could also do that. The only change we would make is to pass another list of items to our
Using One or No Arguments
So far, we have used the
zip() function to merge two iterables together. But we can also use the
zip() function with one or no arguments. If you use the
zip() function with no arguments, you’ll get an empty zip object. Here’s an example:
zipped_object = zip() print(list(zipped_object))
Our code returns an empty iterator:
Our zipped_object result list contains no values because we did not pass any arguments to our
On the other hand, if we were to pass one argument to our zip function, we could use the following code:
employee_names = ["Candice", "Ava", "Andrew", "Lucas"] zipped_object = zip(employee_names) print(list(zipped_object))
Our program returns the following:
[('Candice',), ('Ava',), ('Andrew',), ('Lucas',)]
Passing one argument to a
zip() has very few uses—you could just use your existing list and convert it to a tuple—but it does demonstrate that
zip() can work with only one value.
Looping Over Iterables
Working with multiple iterables is one of the most popular use cases for the
zip() function in Python. For example, if you want to go through multiple lists, you may want to make use of the
zip() function. Indeed, we can use the
zip() function as an iterator of tuples, or any other iterable elements, which is a common operation in software development.
Here is an example of
zip() being used to iterate over multiple arrays:
employee_numbers = [2, 9, 18, 28] employee_names = ["Candice", "Ava", "Andrew", "Lucas"] for name, number in zip(employee_names, employee_numbers): print(name, number)
Our code returns the following:
Candice 2 Ava 9 Andrew 18 Lucas 28
In this example, our program iterates through the list of tuples that
zip() returns, and divides them into two values: name and number. This makes it easy for us to iterate through multiple iterable objects at once. If we wanted to, we could use this to iterate through three or more iterable objects.
In our code, we have zipped various types of data. But how do we restore data to its previous form? If you have a list of tuples—or zipped values—that you want to divide, you can use the
zip() function’s unpacking operator. This is an asterisk
* used in conjunction with the
Here is an example of the
zip() unpacking operator in action:
employees_zipped = [('Candice', 2), ('Ava', 9), ('Andrew', 18), ('Lucas', 28)] employee_names, employee_numbers = zip(*employees_zipped) print(employee_names) print(employee_numbers)
Our code returns the following output:
('Candice', 'Ava', 'Andrew', 'Lucas') (2, 9, 18, 28)
This may seem complicated, so let’s break it down. On the first line, we define a variable that includes a list of tuples. Then, on the next line, we define two variables—
employee_numbers—and assign them values from our unzip function.
The unzip function is simply a zip function that takes our
employees_zipped variable and uses the unpacking operator
*, as seen above. Then, we print out our two new variables that contain our employee names and employee numbers.
In this tutorial, we have broken down how to use the
zip() function in Python.
Zip() receives iterable objects as an input and returns an iterator that can create tuples by pairing elements from each iterable input. This can be useful if you have two or more arrays or tuples that you want to merge into one.
We have also discussed how to use the
zip() function with zero or one inputs, how you can use
zip() to loop over iterables, and how to unzip zipped data using the unpacking operator. You are now on your way to becoming a master of the Python