STEM

Zip Function in Python: How and Why

The zip() function in Python is very useful, versatile, and efficient for combining elements from multiple iterables (like lists or tuples) into pairs or tuples. No, it’s not the file zipping functions most are familiar with in Windows; despite the same name, it’s an entirely different function for different purposes.


Here are some practical scenarios where zip() can be handy.

  1. Pairing Elements from Two Lists
    Imagine we have two lists (aka arrays in other languages): one with names and another with corresponding ages. We want to pair each name with its age.
  2. Iterating Over Multiple Lists Simultaneously
    If we need to perform operations on elements from multiple lists at the same time.
  3. Creating a Dictionary from mulitple Lists
    Dictionaries are amazingly useful and we can use zip() to create a dictionary from two or more lists: one for keys and one for values.

Without zip() function, we’d have to manually use indexing from each list and combine them making sure they’re in the same order. With zip, it’s easy and efficient.

But there are moreā€¦

  1. Transposing a Matrix
    If we have a matrix (a list of lists) and we want to transpose it (convert rows to columns and vice versa).
  2. Synchronizing Iterables with Different Lengths
    Sometimes, we might want to synchronize multiple iterables of different lengths.
    By default, zip() stops creating tuples when the shortest input iterable is exhausted, but we can use itertools.zip_longest with zip to handle that situation.

Examples

Application for these are shown below. On each widget, click on the Run icon to execute the script and see the output on the right pane. You can also modify the original code on the left pane, and click the Run icon again to execute your edited script. If you have errors in any your edits, or need to reset to my original script, click on the menu (hamburger icon, highlights in a blue box when clicked) on the particular widget’s menu bar, it’ll open up the full menu, then click Reset from there and confirm (see image).

Example 1: Pairing elements from two lists

In Example 1, we have two independent lists; one containing names as strings, another containing ages as integers. By using zip() we ‘connect’ them together such that each element of one list gets associated with element of the other list in the exact order of their respective appearance. Then we can easily see each individual’s name and age either as a list or a dictionary as shown. The dict() function converts them to a dictionary, such that name becomes the key and age becomes the value for each item. Then we can query a person’s name by name, such as [‘Bob’] and get his age, 30.

Example 2: Iterating over multiple lists simultaneously

In Example 2, we have 3 lists. We want to do some calculation such as: a*b + c where a, b, c are elements in list1, list2, list3 respectively. We can do all this easily by zipping the lists and simply applying the formula. It multiplies an element of list1 and list2 and adds the element from list3 to the product, and it does so in order, iteratively without having to explicitly index or specifyl the sizes of the lists. Very handy.

Example 3a: Creating a dictionary and using zip

In Example 3a, we create a dictionary using zip() and dict() from 2 lists: keys and values. They keys list contains name, age, and city. Where the values list contains their respective values. After zipping them and getting a new dictionary, we get: Alice, 25, New York (for name, age, city).

Then we add new keys, or fields, for occupation, and hobby. And their respective values: ‘Engineer’, ‘Yoga’ from 2 additional lists. Then we create a dictionary out of them, and update our first dictionary with the new dictionary. So, we get: Alice, 25, New York, Engineer, Yoga (for name, age, city, occupation, hobby).

Then we declare additional persons using all the keys so far from an updated list keys: keys = ["name", "age", "city", "occupation", "hobby"]

And declare the multiple values for multiple persons as a list of lists first
values_list = [
["Alice", 25, "New York", "Engineer", "Yoga"],
["Bob", 30, "Los Angeles", "Clerk", "Reading"],
["Charlie", 45, "Chicago", "Dentist", "Hiking"]
]

and declare a new list to hold dictionaries for each person: persons_list = []. And Create dictionaries for each person and add to the list:

for values in values_list:
person_dict = dict(zip(keys, values)) # <--- This is powerful indeed!
persons_list.append(person_dict)

At this point, person_dict is an updated dictionary, and persons_list is a list (an array) of dictionary. Finally, we print out the final dictionary content from the list, one item at a time, which looks like this:

{‘name’: ‘Alice’, ‘age’: 25, ‘city’: ‘New York’, ‘occupation’: ‘Engineer’, ‘hobby’: ‘Yoga’}
{‘name’: ‘Bob’, ‘age’: 30, ‘city’: ‘Los Angeles’, ‘occupation’: ‘Clerk’, ‘hobby’: ‘Reading’}
{‘name’: ‘Charlie’, ‘age’: 45, ‘city’: ‘Chicago’, ‘occupation’: ‘Dentist’, ‘hobby’: ‘Hiking’}

Very powerful stuff.

Example 3b: Creating a dictionary from three lists

In Example 3b, we create a dictionary out of 3 lists: names, ages, genders. To do this, we first combine the lists using zip(). The zipped list will contain the values for our dictionary that we create next. But for the new dictionary, we need to define the keys, which we do as: “name”, “age”, “gender”. Then we zip the keys and the values from combined_list and create the final dictionary in one fell swoop with this incredibly powerful line: combined_dict = [dict(zip(keys, values)) for values in combined_list] Take a moment to appreciate how many things are actually occuring in this statement!

Example 4: Transposing a matrix

In Example 4, we’re unpacking a matrix and zipping them back together, essentially transposing each element of the matrix! The matrix defined as: matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
] Then would become this matrix: [(1, 4, 7), (2, 5, 8), (3, 6, 9)] when transposed as the first element of the first row stays the same, then the first element of the second row becomes the second element of the first row, then the third element of the third row becomes the third element of the first row, and so on.

This powerful and versatile operation is achieved and finally converted to a list simply by this powerful statement: list(zip(*matrix))

The asterisk is for unpacking the original matrix elements.

Example 5: Synchronizing iterables with different lengths

So far, you may have noticed that the lists we combined were all of the same length. This is a requirement for most operations. By design, the zip() function stops iteration immedately after exhausting the shortest list when dealing with multiple lists. However, there are facilities to deal with lists of different lengths and one of the techniques is to synchronize them, which involves explicitly continuing to iterate even when a list is exhausted but others haven’t been, and specifying what to do in those circumstances.

Meet zip_longest() function! It doesn’t stop until the longest list is exhausted. zip_longest() fills the shorter lists’s missing elements with the special Python value: None. In order to use this function, we have to import it from itertools library. That’s what we are doing in Example 5. First we combine 2 lists: list1 and list2 of different lengths. When you run it you’ll see that although originally there was no third element in list2, it gets the value None after zipping them with zip_longest(). In the following lines, we zip with yet another longer list, list3. And you’ll see in the output where None values appear after combining all the lists.

Example 6: Unzipping or splitting a list of tuples by using zip with unpacking

Why stop at lists and dictionaries? Yes, we can use zip with tuples, list of tuples too. All of the above methods will work on tuples as well. Example 6 demonstrates a simple example of using a list of tuples and using zip() with unpacking to do exact opposite of zipping that we’d been doing previously. We split the numbers and letters from the list of tuples such that we get a tuple of numbers only: (1,2,3) and a tuple of letters only: (‘a’,’b’,’c’).

I hope these examples give you a broader understanding of the incredible power of the zip function and its cohorts. Enjoy exploring more on your own.

I hope you found this post helpful and interesting. Explore this site for more tips and articles. Be sure to also check out my Patreon site where you can find free downloads and optional fee-based code and documentation. Thanks for visiting!

Back To Top