Skip to content
Home » How to Use the zip Function in Python

How to Use the zip Function in Python

Spread the love

A commonly used function in Python is the zip function. You can use it to turn two lists of equal length into a dictionary. Naturally, it only makes sense if the elements of one list can be turned to keys and the elements of the other list to values of a dictionary.

So, what we want to achieve is turn the following two lists:

countries = ["Germany", "Japan", "Australia"]
capitals = ["Berlin", "Tokyo", "Canberra"]

into the following dictionary:

{'Germany': 'Berlin', 'Japan': 'Tokyo', 'Australia': 'Canberra'}

So,  you have two lists: one with countries and the other with their capitals. They’re good candidates to be turned into a dictionary.

for Loop

Actually, there are several ways you can do it. You can achieve that without the zip function, for example by using the for loop like so:

countries = ["Germany", "Japan", "Australia"]
capitals = ["Berlin", "Tokyo", "Canberra"]

a = {}

for n in range(len(countries)):
    a[countries[n]] = capitals[n]

print(a)

The output is:

{'Germany': 'Berlin', 'Japan': 'Tokyo', 'Australia': 'Canberra'}

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

zip Function with Iterator

Another approach is to use the zip function and an iterator: Here is the function with two arguments, which are the two lists:

it = zip(countries, capitals) # it - an iterator

The function returns a list iterator. Then we have to use the list function to convert the list iterator into a list:

pairs = list(it)

If we now print pairs in interactive mode, we’ll get a list of 2-tuples:

>>> pairs
[('Germany', 'Berlin'), ('Japan', 'Tokyo'), ('Australia', 'Canberra')]

We can convert this list into a dictionary using the dict(list) function:

print(country_capital_dict)

Here’s the output:

{'Germany': 'Berlin', 'Japan': 'Tokyo', 'Australia': 'Canberra'}

Here is the full code:

countries = ["Germany", "Japan", "Australia"]
capitals = ["Berlin", "Tokyo", "Canberra"]

it = zip(countries, capitals) # it - an iterator
pairs = list(it)  # list of 2-tuples
country_capital_dict = dict(pairs)  # dictionary

print(country_capital_dict)

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

zip Function Without Iterator

We can achieve the same in a simpler way. We can pass zip as the argument of the dict function:

countries = ["Germany", "Japan", "Australia"]
capitals = ["Berlin", "Tokyo", "Canberra"]

country_capital_dict = dict(zip(countries, capitals))  # dictionary

print(country_capital_dict)

There is one thing we have to know about the zip function. It returns an iterator, not a list. In iterators the elements get used up, so that they can’t be used again. Here we have two lists again:

countries = ["Germany", "Japan", "Australia"]
capitals = ["Berlin", "Tokyo", "Canberra"] 

Let’s zip them:

>>> it = zip(countries, capitals)

Now we can use the iterator to make a list:

>>> list1 = list(it)

And now let’s make another list using the same iterator:

>>> list2 = list(it)

The iterator gets used up when it is used in the first list, so the elements go to the first list:

>>> list1
[('Germany', 'Berlin'), ('Japan', 'Tokyo'), ('Australia', 'Canberra')]

and there is nothing left for the second list. The second list is empty:

>>> list2
[]

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version:


Spread the love

Leave a Reply