Access a dictionary key by index in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Access a dictionary key by index in Python #

Use the list() class to access a dictionary key by index, e.g. key = list(my_dict)[0]. The list() class will convert the dictionary to a list of keys which enables us to access the key at the specific index.

main.py
my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, } # ✅ access dictionary key by index key = list(my_dict)[1] print(key) # 👉️ name # ✅ access dictionary value by index value = list(my_dict.values())[1] print(value) # 👉️ Bobbyhadz # ------------------------------------- # ✅ find index of key in dictionary index = None if 'name' in my_dict: index = list(my_dict).index('name') print(index) # 👉️ 1

We used the list() class to convert the dictionary to a list of keys.

main.py
my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, } print(list(my_dict)) # 👉️ ['id', 'name', 'age'] print(list(my_dict.keys())) # 👉️ ['id', 'name', 'age']

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 = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, } key = list(my_dict)[1] print(key) # 👉️ name value = list(my_dict.values())[1] print(value) # 👉️ Bobbyhadz
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.

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

main.py
my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, } index = None if 'name' in my_dict: index = list(my_dict).index('name') print(index) # 👉️ 1 key = list(my_dict)[index] print(key) # 👉️ name value = list(my_dict.values())[index] print(value) # 👉️ Bobbyhadz
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.

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( [('id', 1), ('name', 'Bobbyhadz'), ('age', 30)] ) key = list(my_dict)[1] print(key) # 👉️ name value = list(my_dict.values())[1] print(value) # 👉️ Bobbyhadz index = None if 'name' in my_dict: index = list(my_dict).index('name') print(index) # 👉️ 1

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.