Find a Key by Value in a Python Dictionary

Recently, I was working on a Python project where I needed to search for a key based on its value in a dictionary.

Since dictionaries are one of the most powerful and frequently used data structures in Python, I decided to explore different ways to solve this problem.

In this tutorial, I’ll share five simple methods I use to find a key by value in a Python dictionary. These methods are practical, easy to understand, and can be applied to real-world projects.

Method 1 – Use a For Loop with items()

The easy way to find a key by value in a Python dictionary is by using a simple for loop. This method works well when you want to search through the dictionary step by step.

# Python program to find key by value using for loop
student_grades = {
    "Alice": 85,
    "Bob": 90,
    "Charlie": 78,
    "David": 90
}

search_value = 90
keys_found = []

for key, value in student_grades.items():
    if value == search_value:
        keys_found.append(key)

print(f"Keys with value {search_value}: {keys_found}")

You can see the output in the screenshot below.

get key from value python

In this example, I loop through each key-value pair using items(). If the value matches, I add the key to a list. This approach is simple and works well if you’re just starting with Python.

Method 2 – Use List Comprehension

List comprehension in Python makes the code shorter and more elegant. Instead of writing multiple lines, you can achieve the same result in one line.

# Python program to find key by value using list comprehension
student_grades = {
    "Alice": 85,
    "Bob": 90,
    "Charlie": 78,
    "David": 90
}

search_value = 90
keys_found = [key for key, value in student_grades.items() if value == search_value]

print(f"Keys with value {search_value}: {keys_found}")

You can see the output in the screenshot below.

find in dictionary python

This method is more Pythonic and is often used by experienced developers. It’s efficient and easy to read once you’re comfortable with list comprehensions.

Method 3 – Use the next() Function

Sometimes, you only want to find the first key that matches a given value. In that case, the next() function is a great choice.

# Python program to find the first key by value using next()
student_grades = {
    "Alice": 85,
    "Bob": 90,
    "Charlie": 78,
    "David": 90
}

search_value = 90
key_found = next((key for key, value in student_grades.items() if value == search_value), None)

print(f"First key with value {search_value}: {key_found}")

You can see the output in the screenshot below.

python dictionary find key by value

Here, I use a generator expression inside next(). If no match is found, it returns None. This is a clean and efficient way when you only need one key.

Method 4 – Use Dictionary Comprehension

Dictionary comprehension is another Python feature that can help us create a new dictionary with only the matched key-value pairs.

This is useful if you want to filter out only the items with the required value.


# Python program to find key by value using dictionary comprehension
student_grades = {
    "Alice": 85,
    "Bob": 90,
    "Charlie": 78,
    "David": 90
}

search_value = 90
matched_dict = {key: value for key, value in student_grades.items() if value == search_value}

print(f"Dictionary with value {search_value}: {matched_dict}")https://pythonguides.com/python-dictionary-of-lists/# Python program to find key by value using dictionary comprehension
student_grades = {
    "Alice": 85,
    "Bob": 90,
    "Charlie": 78,
    "David": 90
}

search_value = 90
matched_dict = {key: value for key, value in student_grades.items() if value == search_value}

print(f"Dictionary with value {search_value}: {matched_dict}")

In this example, I create a new dictionary that contains only the keys with the matching value. This method is useful when you want both keys and values in the output.

Method 5 – Use a Custom Function

If you need to reuse this logic often, writing a custom Python function is a good idea. This makes the code reusable and easy to maintain.

# Python program with a custom function to find keys by value
def find_keys_by_value(dictionary, search_value):
    return [key for key, value in dictionary.items() if value == search_value]

student_grades = {
    "Alice": 85,
    "Bob": 90,
    "Charlie": 78,
    "David": 90
}

result = find_keys_by_value(student_grades, 90)
print(f"Keys with value 90: {result}")

By wrapping the logic inside a function, I can call it anytime without repeating the code. This is especially useful in larger Python projects where dictionary lookups are common.

Real-World Example: Mapping State Abbreviations

Let’s take a real-world example that’s more relevant in the USA.

Imagine you have a dictionary that maps U.S. state abbreviations to their full names, and you want to find the abbreviation by providing the state name.

# Python program to find state abbreviation by state name
us_states = {
    "CA": "California",
    "TX": "Texas",
    "NY": "New York",
    "FL": "Florida",
    "IL": "Illinois"
}

search_state = "Texas"
abbreviation = next((abbr for abbr, name in us_states.items() if name == search_state), None)

print(f"The abbreviation for {search_state} is: {abbreviation}")

This example is practical because it shows how Python dictionaries can be used in real-life applications such as mapping states, ZIP codes, or product IDs.

It also demonstrates how easy it is to adapt the same logic for different datasets.

Things to Keep in Mind

  • A Python dictionary can have duplicate values but not duplicate keys.
  • If multiple keys share the same value, you’ll need to decide whether to return one key or all of them.
  • For large dictionaries, using next() is faster if you only need the first match.
  • If performance is critical, consider using a reverse dictionary (value-to-key mapping).

Finding a key by value in a Python dictionary is not built-in, but as you’ve seen, there are multiple ways to do it.

I personally use list comprehensions and the next() function most often, depending on whether I need one key or multiple keys.

Each method has its use case, and as a Python developer, you’ll find yourself switching between them depending on the problem at hand.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.