Convert List to Dictionary in Python (3 Examples)
Hi! This tutorial will show you 3 simple ways to convert lists into dictionaries in the Python programming language.
First, though, here’s an overview of this tutorial:
Let’s dive right into it!
Create List Examples
Here, we will create the lists that we will convert into a dictionary in this tutorial. Therefore, run the lines of code below in your preferred Python IDE:
keys = ["name", "age", "gender"] values = ["John", 25, "male"]
The keys list has three string items, and the values list has one integer and two string elements.
Example 1: Turn Lists into Dictionary Using dict() & zip() Functions
In this first example, we will use the dict()
and zip()
functions to convert the lists into a Python dictionary called person as follows.
person = dict(zip(keys, values)) print(person) # {'name': 'John', 'age': 25, 'gender': 'male'}
The zip()
function in Python takes two or more iterables as arguments and returns an iterator of tuples, where the first tuple item is from the first iterable, the second tuple item is from the second iterable, and so on.
Example 2: Turn Lists into Dictionary Using for Loop
In this second example, we will use a for loop to create a dictionary from the lists.
person = {} for i in range(len(keys)): person[keys[i]] = values[i] print(person) # {'name': 'John', 'age': 25, 'gender': 'male'}
We simply looped through the keys and values lists and used their content to form the person dictionary, as seen above.
Example 3: Turn Lists into Dictionary Using Dictionary Comprehension
In this third and final example, we will use dictionary comprehension in order to form a dictionary from the keys and values lists.
person = {keys[i]: values[i] for i in range(len(keys))} print(person) # {'name': 'John', 'age': 25, 'gender': 'male'}
So, there you have the person dictionary! We have explored 3 methods of converting lists into a Python dictionary.
I hope you found this helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert 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 how to convert 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:
- Convert Lists to pandas DataFrame in Python (3 Examples)
- Access Dictionary within List in Python (Example)
- Check if List of Lists is Empty in Python (2 Examples)
- Sort List of datetime Objects in Python (Example)
- Learn Python Programming
This post has shown how to convert lists into a dictionary in Python. In case you have further questions, you may leave a comment below.
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.
Statistics Globe Newsletter