How to index a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

How to index a dictionary in Python #

Use the list() class to index a dictionary, e.g. list(my_dict)[0] and list(my_dict.values())[0]. The list() class converts the dictionary or the dictionary's values to a list that we can access at a specific index.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } # ✅ get key in dictionary by index key = list(my_dict)[0] print(key) # 👉️ site # ✅ get value in ditionary by index value = list(my_dict.values())[0] print(value) # 👉️ bobbyhadz.com # -------------------------------------- # ✅ get index of key in dictionary index = None if 'site' in my_dict: index = list(my_dict).index('site') print(index) # 👉️ 0

We used the list() class to index a dictionary.

The list() class converts the dictionary to a list of keys.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } print(list(my_dict)) # 👉️ ['site', 'color', 'number'] print(list(my_dict.keys())) # 👉️ ['site', 'color', 'number']

We could have also used the dict.keys() method to be more explicit.

The dict.keys method returns a new view of the dictionary's keys.

The last step is to access the list of keys at the specific index.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } key = list(my_dict)[0] print(key) # 👉️ site
Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

If you need to get the value at a specific index in the dictionary, pass the result of calling dict.values() to the list() class.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } value = list(my_dict.values())[0] print(value) # 👉️ bobbyhadz.com

The dict.values method returns a new view of the dictionary's values.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } # 👇️ dict_values(['bobbyhadz.com', 'blue', 100]) print(my_dict.values())

The dict.values() method returns a view object which isn't subscriptable (cannot be accessed at an index), so we still had to convert it to a list.

Use the list.index() method if you need to get the index of a specific key or value in the dictionary.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } index = None if 'site' in my_dict: index = list(my_dict).index('site') print(index) # 👉️ 0
The list.index() method returns the index of the first item whose value is equal to the provided argument.

The method raises a ValueError if there is no such item in the list.

We used an if statement to check if the key exists in the dictionary, so the list.index() method will never throw a ValueError.

You can use the same approach to get the index of a value in the dictionary.

main.py
my_dict = { 'site': 'bobbyhadz.com', 'color': 'blue', 'number': 100 } index = None dict_values = my_dict.values() if 'bobbyhadz.com' in dict_values: index = list(dict_values).index('bobbyhadz.com') print(index) # 👉️ 0
As of Python 3.7, the standard dict class is guaranteed to preserve the insertion order of keys.

If you use an older version, use the OrderedDict class instead.

main.py
from collections import OrderedDict my_dict = OrderedDict( [('site', 'bobbyhadz.com'), ('color', 'blue'), ('number', 100)] ) key = list(my_dict)[0] print(key) # 👉️ site value = list(my_dict.values())[0] print(value) # 👉️ bobbyhadz.com # -------------------------------------- index = None if 'site' in my_dict: index = list(my_dict).index('site') print(index) # 👉️ 0

The list() class can also be used to convert the keys of an OrderedDict to a list.

Note that using the OrderedDict class is only necessary if you use a version older than Python 3.7.

Otherwise, use the native dict class as it preserves insertion order.

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.