Create Dictionary from Two Lists in Python (2 Examples)

 

This post demonstrates how to convert two lists into a dictionary in the Python programming language.

The post is structured as follows:

So let’s dig in!

 

Creation of Example Data

To use in this tutorial, I will create three lists. The first two will be in the same length whereas the third one is shorter.

We will see how to merge two lists to form a dictionary when they have the same and different lengths.

keys = ['a', 'b', 'c', 'd']                                       # create keys list
values = [1, 2, 3, 4]                                             # create values list
values_2 = [1, 2, 3]                                              # create second values list

With our samples list created, let’s move on to the first example where we’ll combine lists keys and values to create a dictionary.

 

Example 1: Build Dictionary from Two Lists in Same Length

In Example 1, I’ll demonstrate how to merge two lists of the same length to create a dictionary step by step.

First, we will make use of the zip function to pair the elements of keys and values.

zipped = zip(keys, values)                                        # zip keys and values

Next, we will convert the paired elements from zipped iterator into a dictionary using dict.

my_dict = dict(zipped)                                            # convert to dictionary

Finally, we can print our created dictionary my_dict as follows.

print(my_dict)                                                    # print my_dict
# {'a': 1, 'b': 2, 'c': 3, 'd': 4}

As you can see, our attempt successfully worked. Above, the operation was divided into steps, however, one could also write the whole code in a single line. Please see below.

my_dict = dict(zip(keys, values))                                 # all code in one line
print(my_dict)
# {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Now we know how we can form a dictionary from two lists of the same length. The next step is to do the same with lists of different lengths using keys and values_2 lists.

 

Example 2: Build Dictionary from Two Lists in Different Lengths

In this section, we will utilize the zip_longest function of the itertools package to fill the gaps when two lists have different lengths.

Let’s first import the function!

from itertools import zip_longest                                 # import zip_longest function

Once the funciton is imported, we can use it similarly to the zip function.

You will see that an additional argument, fillvalue, is specified. This allows you to specify the value used for padding when one list is shorter than the other.

my_dict_miss = dict(zip_longest(keys, values_2, fillvalue = None))  # create dictionary with missingness
print(my_dict_miss)                                                 # print my_dict_miss
# {'a': 1, 'b': 2, 'c': 3, 'd': None}

We set the default fill None value to None, for demonstration. Since there is no corresponding element in values_2 for the element "d" in keys, it’s been paired with None.

 

Video & Further Resources

If you need more information on the Python programming code of this tutorial, you might watch the following video on my YouTube channel. I explain the topics of this tutorial in the video.

 

The YouTube video will be added soon.

 

In addition, you might read the related tutorials on this website. I have released several articles already.

 

You have learned on this page how to build a dictionary from two lists in the Python programming language. Tell me about it in the comments, if you have any further questions.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top