Convert List to Dictionary with Index in Python (2 Examples)
Hi! This short tutorial will show you two ways to turn a list into a dictionary with index in the Python programming language.
First, though, here is an overview of this tutorial:
Let’s get right into the Python code!
Create Sample List
Here, we will create the sample Python list that we will turn into a dictionary with index. So, in your Python IDE, run the line of code below to create the sample list:
sample_list = ["apple", "banana", "cherry", "date"]
Our sample list, called sample_list, contains four string elements. Let’s see now how to convert it to a dictionary with indices in the next sections!
Example 1: Transform List to Dictionary with Index Using enumerate() Function
In this first example, we will use Python’s built-in enumerate() function to transform the list into a dictionary with indices.
my_dict = dict(enumerate(my_list)) print(my_dict) # {0: 'apple', 1: 'banana', 2: 'cherry', 3: 'date'} print(type(my_dict)) # <class 'dict'>
The enumerate()
function iterates through the items in the list while keeping track of their respective index positions. In the code above, the enumerate()
function returns the index positions of each item in the list along with the values, while the dict()
function turns that into a dictionary.
Example 2: Transform List to Dictionary with Index Using Dictionary Comprehension
In this second example, we will use dictionary comprehension to turn the list to a dictionary with indices.
my_dict = {i: my_list[i] for i in range(len(my_list))} print(my_dict) # {0: 'apple', 1: 'banana', 2: 'cherry', 3: 'date'} print(type(my_dict)) # <class 'dict'>
The dictionary comprehension method above specified the key-value pairs concisely, with the index positions as the key and the elements in the list as the values.
With that, we have demonstrated how to turn a list into a dictionary with indices in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to turn a list into a dictionary with index 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 turn a list into a dictionary with index 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 List to Dictionary in Python (3 Examples)
- Convert List to pandas DataFrame in Python (3 Examples)
- Convert List to Range in Python (2 Examples)
- Access Dictionary within List in Python (Example)
- Learn Python Programming
This post has shown how to how to turn a list into a dictionary with index 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