Convert List to Dictionary with Index as Values in Python (2 Examples)

 

Hi! This short tutorial will show you how to transform a list to a dictionary with indexes as values in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example Python List of Strings

Here, we will create the example Python list of strings that will be changed into a dictionary with the indexes as values. Therefore, in your Python coding IDE, run the line of code below to create the example Python list of strings that will be turned into a dictionary in this tutorial:

my_list = ["a", "b", "c", "d", "e"]
 
print(my_list)
 
# ['a', 'b', 'c', 'd', 'e']
 
print(type(my_list))
 
# <class 'list'>

With the example list of strings created, we will now explore ways to turn the list to a dictionary with indexes as values.
 

Example 1: Change List to Dictionary Using Dictionary Comprehension & enumerate() Function

In this first example, we will use dictionary comprehension along with the enumerate() function to turn the list to a dictionary with indexes as values:

my_dict = {value:index for index, value in enumerate(my_list)}
 
print(my_dict)
 
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
 
print(type(my_dict))
 
# <class 'dict'>

This method uses a dictionary comprehension to iterate over the list using the enumerate() function, which returns an iterator that yields a tuple of index and value for each element in the list.

The syntax specifies that for each tuple: (index, value) yielded by enumerate(my_list), a new key-value pair should be added to the dictionary, where the list value is the key and the index is the value.

The resulting dictionary is then printed using the print() function.
 

Example 2: Change List to Dictionary with Index as Values Using for Loop

In this second example, we will use a for loop to transform the list to a dictionary with indexes as values:

my_dict = {}
 
for index in range(len(my_list)):
    value = my_list[index]
    my_dict[value] = index
 
print(my_dict)
 
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
 
print(type(my_dict))
 
# <class 'dict'>

Here, an empty dictionary is first created using my_dict = {}. Then, a for loop is used to iterate over the indiexes of my_list using the range() function and the len() function.

Inside the loop, the current value is obtained by indexing into my_list using the current index, value = my_list[index]. Then, a new key-value pair is added to the dictionary with my_dict[value] = index, where the key is the current value and the value is the current index.

Finally, the resulting dictionary is printed using the print() function.

With that, we have successfully demonstrated in this tutorial how to convert a list to a dictionary with indexes as values in Python. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list to a dictionary with indexes as values 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 a list to a dictionary with indexes as values 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 convert a list to a dictionary with index as values 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