Convert Multiple Lists to Dictionary in Python (3 Examples)

 

Hello! This tutorial will show you 3 simple ways to convert multiple lists into a dictionary in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s jump right into the Python code!

 

Create Sample Lists

Here, we will create the sample lists that we will convert into a dictionary in this tutorial. Therefore, in your Python IDE, run the lines of code below to create the sample lists:

keys = ["Name", "Age", "Sex", "Occupation"]
values = ["Chioma", 25, "Female", "Graphic Designer"]

As seen, we have two lists defined under the names: keys and values. Now let’s continue with the first example of the tutorial!

 

Example 1: Transform the Lists into a Dictionary with dict() & zip() Functions

In this example, we will use Python’s dict() function to convert the lists into a dictionary.

my_dict = dict(zip(keys, values))
print(my_dict)
# {'Name': 'Chioma', 'Age': 25, 'Sex': 'Female', 'Occupation': 'Graphic Designer'}
 
 
print(type(my_dict))
# <class 'dict'>

In the code above, we combined the dict() function and the zip() function, which returns an iterator of tuples. As a result, the dictionary object, my_dict was created. Then we confirmed that the data type of the created object is a dictionary by the type() function.

 

Example 2: Transform the Lists into a Dictionary with Dictionary Comprehension

In this second example, we will make use of dictionary comprehension to convert the lists into a dictionary:

my_dict = {keys[i]: values[i] for i in range(len(keys))}
print(my_dict)
# {'Name': 'Chioma', 'Age': 25, 'Sex': 'Female', 'Occupation': 'Graphic Designer'}
 
print(type(my_dict))
# <class 'dict'>

The code iterates through the items in both the keys and values lists and returns a dictionary object as seen.
 

Example 3: Transform the Lists into a Dictionary with For Loop

In this third and final example, we will run a for loop through the lists and convert them into a dictionary, like so:

my_dict = dict()
for i in range(len(keys)):
  my_dict.setdefault(keys[i],values[i])
 
print(my_dict)
#'Chioma'
#25
#'Female'
#'Graphic Designer'
# {'Name': 'Chioma', 'Age': 25, 'Sex': 'Female', 'Occupation': 'Graphic Designer'}

In the above code, we parsed the loop to the setdefault() method, which set each key and value in the lists as the default for the dictionary; and we can, once again, ascertain that the product is a dictionary object by running:

print(type(my_dict))
# <class 'dict'>

The data type is a dictionary; well done!

With that, we have demonstrated 3 simple ways to convert multiple lists into a dictionary in Python. I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to convert multiple lists into a dictionary in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert multiple lists into a dictionary in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to transform multiple lists into a dictionary in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

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