How to Convert Two Lists into a Dictionary in Python: Zip, Comprehensions, and Loops

How to Convert Two Lists Into a Dictionary in Python Featured Image

Welcome to the fourth installment of the How to Python series. Today, we’re going to take a look at how to convert two lists into a dictionary in Python.

In short, there are three main ways to solve this problem. First, try taking advantage of zip and the dictionary constructor (i.e. `dict(zip(keys, values))`python). Otherwise, you might prefer to use a dictionary comprehension (i.e `{key:value for key, value in zip(keys, values)}`python). If neither of these options work for you, you can always build your own loop.

In the following sections, we’ll take a look at each of these solutions in more detail.

Table of Contents

Video Summary

As always, I like to include a video which covers all of the content discussed in this article. If you’re interested, check it out. Otherwise, jump down below!

Problem Introduction

Sometimes iterating over two lists at the same time can be confusing and complicated. If these lists need to be scanned often, it may just make more sense to convert them into a dictionary. That way, we can eliminate traversals and focus on maintaining a single dictionary.

Merging Two Lists into a Dict

In my personal experience, I’ve found that database libraries often return data in a huge matrix or list of lists. This can be really annoying to traverse and maintain, so I sometimes prefer to map the column names to each row of data. For simplicity though, let’s imagine we have two lists:

column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']

And, we want to map the names to the values:

name_to_value_dict = {'id': 1, 'color': 'red', 'style': 'bold'}

Now instead of indexing both lists simultaneously, we can get the information we need by name. For example:

id = name_to_value_dict['id']
color = name_to_value_dict.get('color', 'blue')

In this tutorial, we’ll cover exactly how to convert two lists into a dictionary.

Caveats and Considerations

With all that said, there are a few things to be aware of when mapping one list to another.

First, the key list must contain unique values. Otherwise, we may lose information in the mapping process. For example:

keys = ['water', 'fire', 'earth', 'fire']
values = [15, -5, 10, 10]

What would we expect to happen during the mapping process? We can’t have duplicate keys, so we’re going to have to ditch either the 15 or the 10. Or, perhaps we may have a more sophisticated solution which chooses the larger number or sums them together—more on that later!

Likewise, each list must be the same size. Otherwise, we may again lose some information in the mapping process. For example:

keys = ['parrot', 'chicken', 'tiger']
values = [0, 37, 12, 42, 2]

What would the keys for 42 and 2 be? Again, we may be able to do something more sophisticated based on our needs, but it’s not clear what we would need to do in this situation. Perhaps, we could make generic keys, or maybe we would just truncate those remaining values.

At any rate, keep those rules in the back of your mind.

Solutions

As with most problems, merging two lists into a dictionary has many solutions. Let’s take a look at a few.

Convert Two Lists with Zip and the Dict Constructor

Zip is a great functionality built right into Python. In Python 2Opens in a new tab., zip merges the lists into a list of tuples. In Python 3Opens in a new tab., zip does basically the same thing, but instead it returns an iterator of tuples. Regardless, we’d do something like the following:

column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']
name_to_value_dict = dict(zip(column_names, column_values))

This solution is quick and dirty. If the lists differ in size, this method will truncate the longer list. If the keys are not unique, this method will pick the last value for mapping.

Convert Two Lists with a Dictionary Comprehension

As of Python 2.7 and of course Python 3.0, we have the option to use a dictionary comprehension. Much like the comprehension we used in the first lesson, a dictionary comprehension allows us to generate a dictionary in a single expression. Take a look:

column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']
name_to_value_dict = {key:value for key, value in zip(column_names, column_values)}

As an added bonus, the dictionary comprehension method may perform slightly better than the dict constructor method. Regardless, they do essentially the same thing.

Convert Two Lists with a Loop

While the methods above are nice, they don’t leave a lot of room for customization. As a result, we may need to fall back on something a bit less Pythonic: a loop. That way, we can customize exactly what happens to our keys and values as we build our dictionary. For instance:

column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']
name_value_tuples = zip(column_names, column_values)
name_to_value_dict = {}
for key, value in name_value_tuples:
    if key in name_to_value_dict:
        pass # Insert logic for handling duplicate keys
    else:
        name_to_value_dict[key] = value

While this solution isn’t nearly as compact, it does allow us to handle mapping issues appropriately. We should use this method if we have a one-to-many relationship between our keys and values.

Performance

As usual, I like to store all our solutions in strings to start:

setup = """
column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']
"""

zip_dict = """
name_to_value_dict = dict(zip(column_names, column_values))
"""

dict_comprehension = """
name_to_value_dict = {key:value for key, value in zip(column_names, column_values)}
"""

loop = """
name_value_tuples = zip(column_names, column_values)
name_to_value_dict = {}
for key, value in name_value_tuples:
    if key in name_to_value_dict:
        pass # Insert logic for handling duplicate keys
    else:
        name_to_value_dict[key] = value
"""

With that out of the way, we can begin testing:

>>> import timeit
>>> min(timeit.repeat(stmt=zip_dict, setup=setup, repeat=10))
0.47981049999999925
>>> min(timeit.repeat(stmt=dict_comprehension, setup=setup, repeat=10))
0.5409264999999976
>>> min(timeit.repeat(stmt=loop, setup=setup, repeat=10))
0.532911900000002

As we can see, all three solutions appear to function more or less the same, so it’s up to you which solution you choose.

A Little Recap

With the three methods above, we should easily be able to convert two lists into a dictionary.

column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']

# Convert two lists into a dictionary with zip and the dict constructor
name_to_value_dict = dict(zip(column_names, column_values))

# Convert two lists into a dictionary with a dictionary comprehension
name_to_value_dict = {key:value for key, value in zip(column_names, column_values)}

# Convert two lists into a dictionary with a loop
name_value_tuples = zip(column_names, column_values) 
name_to_value_dict = {} 
for key, value in name_value_tuples: 
    if key in name_to_value_dict: 
        pass # Insert logic for handling duplicate keys 
    else: 
        name_to_value_dict[key] = value

As we can see, there are several ways to map two lists into a dictionary. Take your pick!

If you found this article helpful, there’s plenty more where that came from. For instance, I have a huge list of Python code snippets for you to check out. Otherwise, help this collection grow by becoming a subscriber todayOpens in a new tab. or hopping on the mailing list!

If you don’t want to clutter your inbox but you’d still like to support the site, check out the 5 Ways to Support The Renegade Coder article. In addition, here are a few related articles:

Once again, thanks for your support. I appreciate the help!

How to Python (42 Articles)—Series Navigation

The How to Python tutorial series strays from the usual in-depth coding articles by exploring byte-sized problems in Python. In this series, students will dive into unique topics such as How to Invert a Dictionary, How to Sum Elements of Two Lists, and How to Check if a File Exists.

Each problem is explored from the naive approach to the ideal solution. Occasionally, there’ll be some just-for-fun solutions too. At the end of every article, you’ll find a recap full of code snippets for your own use. Don’t be afraid to take what you need!

If you’re not sure where to start, I recommend checking out our list of Python Code Snippets for Everyday Problems. In addition, you can find some of the snippets in a Jupyter notebook format on GitHubOpens in a new tab.,

If you have a problem of your own, feel free to ask. Someone else probably has the same problem. Enjoy How to Python!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts