How to Convert Two Lists into a Dictionary in Python

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

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

Table of Contents

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.

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 simply 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.

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 2, zip merges the lists into a list of tuples. In Python 3, 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.

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!

Series Navigation← How to Check if a File Exists in PythonHow to Check if a List is Empty in Python →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.