Get dictionary Key by Value in Python

avatar

Borislav Hadzhiev

Last updated: Sep 12, 2022

banner

Photo from Unsplash

Get dictionary Key by Value in Python #

To get a dictionary's key by value:

  1. Use the dict.keys() method to get a view of the dictionary's keys.
  2. Use the dict.values() method to get a view of the dictionary's values.
  3. Use the list.index() method to get the index of the value.
  4. Print the corresponding key.
main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } # ✅ get dictionary key by value (dict.keys() and dict.values()) value = 'bobbyhadz' key = list(my_dict.keys())[list(my_dict.values()).index(value)] print(key) # 👉️ name # --------------------------------------------- # ✅ get dictionary key by value (list comprehension) keys = [k for k, v in my_dict.items() if v == 'bobbyhadz'] print(keys) # 👉️ ['name']

The first example uses the dict.keys() and dict.values() methods.

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

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.keys()) # 👉️ dict_keys(['id', 'name'])

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

main.py
my_dict = {'id': 1, 'name': 'bobbyhadz'} print(my_dict.values()) # 👉️ dict_values([1, 'bobbyhadz'])
We used the list.index() method to get the index of the value bobbyhadz in the dictionary and accessed the list of keys at the index.

We had to convert the view object of keys to a list because view objects are not subscriptable (don't allow index access).

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

We also had to convert the view object to a list to be able to use the list.index() method.

The list.index() method returns the index of the first item whose value is equal to the provided argument.

Alternatively, you can use a for loop.

Get dictionary Key by Value using for loop #

To get a dictionary's key by value:

  1. Use the dict.items() method to get a view of the dictionary's items.
  2. Use a for loop to iterate over the view.
  3. On each iteration, check if the current value is equal to a specific value.
  4. If the condition is met, access the corresponding key.
main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } for key, value in my_dict.items(): if value == 'bobbyhadz': print(key) # 👉️ name

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.items()) # 👉️ dict_items([('id', 1), ('name', 'BobbyHadz')])

On each iteration, we check if the current value is equal to a specific value and if the condition is met, we print the corresponding key.

If you only need to get a single key, use the break statement to avoid unnecessary iterations.
main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'age': 30 } for key, value in my_dict.items(): if value == 'bobbyhadz': print(key) # 👉️ name break

The break statement breaks out of the innermost enclosing for or while loop.

If you need to get all keys with the specified value in the dictionary, append the matching keys to a list.

main.py
my_dict = { 'id': 'bobbyhadz', 'name': 'bobbyhadz', 'age': 30 } keys = [] for key, value in my_dict.items(): if value == 'bobbyhadz': keys.append(key) print(keys) # 👉️ ['id', 'name']

We check if the value of the current iteration is equal to a specific value and if the condition is met, we append the corresponding key to the list of keys.

The list.append() method adds an item to the end of the list.

The method returns None as it mutates the original list.

Alternatively, you can use a list comprehension.

Get dictionary Key by Value using list comprehension #

To get a dictionary's key by value:

  1. Use a list comprehension to iterate over the dictionary's items.
  2. On each iteration, check if the current value is equal to a specific value.
  3. If the condition is met, return the matching key.
main.py
my_dict = { 'id': 'bobbyhadz', 'name': 'bobbyhadz', 'age': 30 } keys = [k for k, v in my_dict.items() if v == 'bobbyhadz'] print(keys) # 👉️ ['name']

We used a list comprehension to iterate over the dictionary's items.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current value is equal to a specific value.

If the condition is met, we return the corresponding key.

Using a list comprehension is only suitable if you need to get all keys that point to the specified value.

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.