HOWTO · Python

How to Find Key by Value in Python Dictionary

Find the key or keys for a value in a Python dictionary. Compare a loop, next(), a list comprehension, and aligned key and value lists, including duplicate and missing-value behavior.

On this page

Python dictionaries map keys to values, so finding a key from a value requires searching the dictionary’s entries. Iterate over each key–value pair and compare its value with the target. Use a loop or next() when you need the first matching key, and a list comprehension when you need every matching key. These searches scan entries until a match is found or the dictionary is exhausted.

Search key–value pairs with a loop

dict.items() provides an iterable of key–value pairs. A loop over it can return the first key whose value equals the target. Returning None after the loop gives callers a clear result when no match exists; if None could itself be a valid key, use a distinct sentinel or return a (found, key) pair instead.

Run this example:

def find_key_by_value(dictionary, target):
    for key, value in dictionary.items():
        if value == target:
            return key
    return None

scores = {"Ada": 91, "Lin": 87, "Sam": 91}
print(find_key_by_value(scores, 87))
Lin

The loop returns immediately on a match, so it returns the first matching key in dictionary iteration order. If several keys have the target value, later matches are not visited after the first one.

Get the first matching key with next()

A generator expression can express the same first-match search compactly. Pass it to next() with a default value so the call returns None when the generator has no matching key, instead of raising StopIteration.

Define the target as an argument so the expression works for either a matching or nonmatching value:

def find_key_with_next(dictionary, target):
    return next((key for key, value in dictionary.items() if value == target), None)

scores = {"Ada": 91, "Lin": 87, "Sam": 91}
print(find_key_with_next(scores, 87))
print(find_key_with_next(scores, 100))

The generator produces keys as needed, so it does not build a list of every match. The default is returned when no value matches. Choose a default that cannot be confused with a valid key if your data may contain None as a key.

Collect all matching keys with a comprehension

When duplicate values matter, collect every matching key with a list comprehension:

def find_all_keys(dictionary, target):
    return [key for key, value in dictionary.items() if value == target]

scores = {"Ada": 91, "Lin": 87, "Sam": 91}
print(find_all_keys(scores, 91))
['Ada', 'Sam']

The returned list is empty when no value matches and contains all matching keys otherwise. A filter() expression can also select matching key–value pairs, but the comprehension makes the condition and the collected keys visible in one place.

Find a key through aligned key and value lists

Another option is to convert dict.keys() and dict.values() to lists. Their positions correspond: the key at position i belongs to the value at position i. Calling index() on the values list finds the first position containing the target; use that position to select the key from the keys list.

scores = {"Ada": 91, "Lin": 87, "Sam": 91}
keys = list(scores.keys())
values = list(scores.values())
position = values.index(91)
print(keys[position])
Ada

index() raises ValueError if the value is absent, so check membership in values first or handle that error when absence is expected. With duplicate values, index() selects only the first matching position.

Handle duplicates, missing values, and performance

Choose the result shape based on the task: a loop or next() returns one key, while the comprehension returns a list of all matching keys. The first-match approaches and index() stop at the first match; their result depends on dictionary iteration order. A no-match result can be None or an empty list, while the aligned-list technique raises ValueError unless you check for membership or handle the error.

All these approaches may inspect every entry, so a search takes time proportional to the number of dictionary entries in the worst case. The loop and generator expression do not create a collection of matches. The comprehension creates a result list, and the aligned-list approach creates separate key and value lists before searching. If repeated value-to-key lookups are needed, consider maintaining a reverse mapping; account for duplicate values because a simple reverse dictionary keeps only one key per value. For nested dictionaries, first decide which level should be searched, then apply the same entry-iteration pattern to that dictionary.