Get keys from a dictionary by value in Python

Modified: | Tags: Python, Dictionary

This article explains how to get keys from a dictionary (dict) by value in Python.

To get a value from a dictionary by key, see the following article.

You can get all the keys in a dictionary as a list using the list() function, since a dictionary iterates through its keys.

d = {'key1': 1, 'key2': 2, 'key3': 3}

print(list(d))
# ['key1', 'key2', 'key3']

Get keys from a dictionary by value using list comprehension and items()

To get keys from a dictionary by value, use list comprehension and the items() method.

For more information on list comprehensions and using for loops with dictionaries, refer to the following articles.

The following example demonstrates how to get a list of keys associated with a specified value. If no key with the specified value exists, an empty list is returned.

d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}

keys = [k for k, v in d.items() if v == 'aaa']
print(keys)
# ['key1', 'key2']

keys = [k for k, v in d.items() if v == 'bbb']
print(keys)
# ['key3']

keys = [k for k, v in d.items() if v == 'xxx']
print(keys)
# []

To get the key itself instead of the list, you can access the first element of the list using [0]. However, if no key with the specified value exists, the list will be empty. In that case, specifying [0] will raise an IndexError.

key = [k for k, v in d.items() if v == 'aaa'][0]
print(key)
# key1

key = [k for k, v in d.items() if v == 'bbb'][0]
print(key)
# key3

# key = [k for k, v in d.items() if v == 'xxx'][0]
# print(key)
# IndexError: list index out of range

If you need to perform the same operation multiple times, it's helpful to define a function.

def get_keys_from_value(d, val):
    return [k for k, v in d.items() if v == val]

keys = get_keys_from_value(d, 'aaa')
print(keys)
# ['key1', 'key2']

The following function can also be used for dictionaries without duplicate values.

If a key with the specified value exists, that key is returned; otherwise, None is returned. If multiple keys have the specified value, one of those keys is returned.

def get_key_from_value(d, val):
    keys = [k for k, v in d.items() if v == val]
    if keys:
        return keys[0]
    return None

key = get_key_from_value(d, 'aaa')
print(key)
# key1

key = get_key_from_value(d, 'bbb')
print(key)
# key3

key = get_key_from_value(d, 'xxx')
print(key)
# None

Examples of extracting keys with various conditions

In the examples above, keys with values equal to the specified value are extracted.

To extract keys under different conditions, modify the condition in the list comprehension.

d_num = {'key1': 1, 'key2': 2, 'key3': 3}

keys = [k for k, v in d_num.items() if v >= 2]
print(keys)
# ['key2', 'key3']

keys = [k for k, v in d_num.items() if v % 2 == 1]
print(keys)
# ['key1', 'key3']

d_str = {'key1': 'aaa@xxx.com', 'key2': 'bbb@yyy.net', 'key3': 'ccc@zzz.com'}

keys = [k for k, v in d_str.items() if v.endswith('com')]
print(keys)
# ['key1', 'key3']

Related Categories

Related Articles