Convert list to dictionary with indexes as Keys in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Convert list to dictionary with indexes as Keys in Python #

To convert a list to a dictionary with indexes as keys:

  1. Use the enumerate() function to get an object of index and value tuples.
  2. Pass the enumerate object to the dict() class.
  3. The dict class will return a dictionary with indexes as keys.
main.py
my_list = ['bobby', 'hadz', 'com'] # ✅ convert list to dictionary with indexes as keys my_dict = dict(enumerate(my_list)) print(my_dict) # 👉️ {0: 'bobby', 1: 'hadz', 2: 'com'} # ---------------------------------------------------------- # ✅ convert list to dictionary with indexes as values my_dict = {key: value for value, key in enumerate(my_list)} print(my_dict) # 👉️ {'bobby': 0, 'hadz': 1, 'com': 2}

We used the enumerate() function to get an object of tuples.

main.py
my_list = ['bobby', 'hadz', 'com'] # 👇️ [(0, 'bobby'), (1, 'hadz'), (2, 'com')] print(list(enumerate(my_list)))

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

We can pass the enumerate object directly to the dict() class to get a dictionary with indexes as keys.

main.py
my_list = ['bobby', 'hadz', 'com'] my_dict = dict(enumerate(my_list)) print(my_dict) # 👉️ {0: 'bobby', 1: 'hadz', 2: 'com'}

The dict() class can be passed an iterable of key-value pairs and returns a new dictionary.

Alternatively, you can use a dict comprehension.

Convert List to Dictionary with indexes as Keys using dict comprehension #

To convert a list to a dictionary with indexes as keys:

  1. Use the enumerate() function to get an object of index and value tuples.
  2. Use a dict comprehension to iterate over the enumerate object.
  3. Return the indexes as dictionary keys and the items as values.
main.py
my_list = ['bobby', 'hadz', 'com'] # ✅ convert list to dictionary with indexes as keys my_dict = {index: value for index, value in enumerate(my_list)} print(my_dict) # 👉️ {0: 'bobby', 1: 'hadz', 2: 'com'} # ---------------------------------------------------------- # ✅ convert list to dictionary with indexes as values my_dict = {value: index for index, value in enumerate(my_list)} print(my_dict) # 👉️ {'bobby': 0, 'hadz': 1, 'com': 2}

We used a dict comprehension to iterate over the enumerate object.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

On each iteration, we return the current index as the key and the item as the value.

If you need to convert the list to a dictionary with indexes as values, switch the return values.

Which approach you pick is a matter of personal preference. I'd go with the dict comprehension as it is more flexible and explicit.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.