|
| Home |
|---|
In a Python dictionary, data is stored as key-value pairs, and normally you use a key to look up its value. Finding a key by its value — sometimes called a reverse lookup — is the opposite operation: you already know the value, and you want to find which key (or keys) it belongs to.
Python dictionaries don't provide a single built-in method for this,
since values aren't indexed the way keys are. In this tutorial you
will learn several ways to find a key by value — using a loop,
list comprehension, and the next() function — how
to handle values that appear under multiple keys, and what to do when
no match is found.
Python dictionaries are implemented using a hash table built on
keys, which is what makes key lookups like dictionary[key]
extremely fast — O(1) on average. Values are not hashed or
indexed the same way, so there is no equivalent fast lookup from
value to key. Finding a key by value always requires checking values
one by one, using a loop or a loop-based expression like a list
comprehension.
The most common and beginner-friendly method is to loop through the dictionary's items and check each value.
for key, value in dictionary.items():
if value == target_value:
print(key)
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
for key, value in student.items():
if value == "Python":
print(key)
course
The program checks each key-value pair in the dictionary. When it finds the value "Python", it prints the key associated with that value — in this case, course.
Unlike dictionary keys, which must be unique, dictionary values can repeat. A loop naturally finds every matching key, not just the first one.
data = {
"a":10,
"b":20,
"c":10
}
for key, value in data.items():
if value == 10:
print(key)
a
c
Both keys a and c contain the value 10, so both are printed. This confirms that dictionaries can store duplicate values even though their keys must always stay unique.
A list comprehension performs the same search in a single line and collects every matching key into a list, which is especially useful when you expect more than one match.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
result = [key for key, value in student.items() if value == "Python"]
print(result)
['course']
The comprehension builds a list of keys where the value equals "Python". Since only one key matches, the result is a list containing a single item. Using a list here (rather than printing directly) makes it easy to check how many matches were found, or to handle the no-match case separately.
If you only need the first matching key and want to
stop searching as soon as one is found, combine a generator
expression with Python's built-in next() function. This
is more efficient than building a full list when a dictionary is
large and you only need one result.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
result = next((key for key, value in student.items() if value == "Python"), None)
print(result)
course
The generator expression (key for key, value in ... if ...)
searches lazily, one item at a time, and next() returns
the first match it finds. The second argument to next(),
None, is the default value returned if no key matches
— without it, next() would raise a
StopIteration error when there is no match.
Any of the above methods can encounter a value that doesn't exist in the dictionary at all. It's important to handle this case explicitly rather than assuming a match will always be found.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
result = [key for key, value in student.items() if value == "Java"]
if result:
print(result)
else:
print("No matching key found")
No matching key found
Checking if result: works because an empty list is
falsy in Python, so this safely detects when no key contains the
target value.
Finding a key by value runs in O(n) time in the worst case, since every key-value pair may need to be checked before a match is found (or ruled out entirely). This is significantly slower than looking up a value by key, which runs in O(1) average time thanks to the dictionary's hash table. If you need to perform reverse lookups frequently on the same dictionary, consider building and maintaining a separate value-to-key dictionary instead of searching repeatedly.
dictionary[value], which looks up by key, not valuenext() without a default value, which raises a StopIteration error if no key matches
No. Square-bracket access always looks up by key. To search by
value, you need to loop through dictionary.items() and
compare each value manually.
A loop or list comprehension will find and return every matching
key. If you only want the first match, use the
next()-based approach instead.
A loop simply finds nothing to print, and a list comprehension
returns an empty list. Always check for this case explicitly, for
example with if result:, rather than assuming a match
will exist.
Yes. Looking up a value by key runs in O(1) average time, while finding a key by value runs in O(n) time, since every value must be checked individually.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net