Search for a Dictionary Key by Value in Python

Working on a Python project that involved analyzing customer data for a retail client, I needed to find which customer ID matched a specific purchase amount. The data was stored in a Python dictionary where keys represented customer IDs and values represented their total purchase amounts.

I assumed there would be a built-in function to search a dictionary by value in Python. But as I quickly found out, there isn’t one. However, I’ve learned that Python’s flexibility always provides a way.

In this tutorial, I’ll show you three simple and effective methods to search a dictionary by value in Python.

Search a Dictionary by Value in Python

In Python, dictionaries are one of the most powerful and flexible data structures. They store data in key-value pairs, allowing quick lookups by key. However, sometimes you need to find the key(s) that correspond to a specific value.

For example, imagine you have a Python dictionary like this:

sales_data = {
    "CUST001": 2500,
    "CUST002": 3200,
    "CUST003": 2500,
    "CUST004": 4100
}

If you want to find which customers spent $2500, you’ll need to search the dictionary by value.

Let’s explore different ways to do this in Python.

Method 1 – Use a For Loop to Search by Value

When I first started working with Python dictionaries, I often used simple loops for lookups. This method is easy to understand, especially for beginners.

Here’s how you can use a for loop to search a dictionary by value in Python:

sales_data = {
    "CUST001": 2500,
    "CUST002": 3200,
    "CUST003": 2500,
    "CUST004": 4100
}

# Value to search for
search_value = 2500

# Empty list to store matching keys
matching_keys = []

# Loop through dictionary items
for key, value in sales_data.items():
    if value == search_value:
        matching_keys.append(key)

print("Customers who spent $2500:", matching_keys)

This code loops through each key-value pair in the dictionary and checks if the value matches the one you’re looking for. If multiple keys share the same value, all of them will be added to the list of matching keys.

Output:

Customers who spent $2500: ['CUST001', 'CUST003']

I executed the above example code and added the screenshot below.

python dictionary search by value

This method is simple, readable, and works well for small to medium-sized dictionaries.

Method 2 – Use List Comprehension in Python

As I gained more experience with Python, I started using list comprehensions to make my code more concise and Pythonic.

List comprehensions are ideal when you want a clean, one-line solution to search a dictionary by value.

Here’s how it works:

sales_data = {
    "CUST001": 2500,
    "CUST002": 3200,
    "CUST003": 2500,
    "CUST004": 4100
}

# Value to search for
search_value = 2500

# Find all keys with the given value using list comprehension
matching_keys = [key for key, value in sales_data.items() if value == search_value]

print("Customers who spent $2500:", matching_keys)

Just like the previous method, this one returns all keys that match the given value.

Output:

Customers who spent $2500: ['CUST001', 'CUST003']

I executed the above example code and added the screenshot below.

python dictionary search

This Python code is more compact and efficient. It’s also easier to maintain when you’re working on larger projects.

Method 3 – Use Lambda Function and Filter()

Another powerful way to search a dictionary by value in Python is by using the filter() function along with a lambda expression.

This method is particularly useful when you prefer functional-style programming or want to chain operations.

Here’s the full example:

sales_data = {
    "CUST001": 2500,
    "CUST002": 3200,
    "CUST003": 2500,
    "CUST004": 4100
}

# Value to search for
search_value = 2500

# Using filter() and lambda to find matching keys
matching_keys = list(
    map(
        lambda item: item[0],
        filter(lambda item: item[1] == search_value, sales_data.items())
    )
)

print("Customers who spent $2500:", matching_keys)

This Python code filters the dictionary items where the value matches search_value and then maps those items back to their keys.

Output:

Customers who spent $2500: ['CUST001', 'CUST003']

I executed the above example code and added the screenshot below.

python search dictionary by value

This approach is elegant and functional, though it might look complex to beginners.

Method 4 – Use Dictionary Comprehension

In some cases, you might want to create a new dictionary that only contains the items matching a specific value. Python’s dictionary comprehension makes this easy.

Here’s how you can do it:

sales_data = {
    "CUST001": 2500,
    "CUST002": 3200,
    "CUST003": 2500,
    "CUST004": 4100
}

# Value to search for
search_value = 2500

# Create a new dictionary with matching key-value pairs
filtered_dict = {key: value for key, value in sales_data.items() if value == search_value}

print("Filtered dictionary:", filtered_dict)

This code creates a smaller dictionary containing only the matching entries.

Output:

Filtered dictionary: {'CUST001': 2500, 'CUST003': 2500}

I executed the above example code and added the screenshot below.

python search dictionary for value

This method is great when you need to work with the filtered results later in your Python program.

Method 5 – Use Next() to Find the First Matching Key

Sometimes, you only need the first key that matches a given value instead of all of them. Python’s next() function is perfect for this scenario.

Here’s how you can use it:

sales_data = {
    "CUST001": 2500,
    "CUST002": 3200,
    "CUST003": 2500,
    "CUST004": 4100
}

# Value to search for
search_value = 3200

# Find the first key that matches the value
first_match = next((key for key, value in sales_data.items() if value == search_value), None)

print("First customer who spent $3200:", first_match)

The next() function stops searching as soon as it finds the first match, making it more efficient for large dictionaries.

Output:

First customer who spent $3200: CUST002

This is one of my go-to methods when performance matters and I only need one result.

Practical Use Case: Search by Value in Real Data

In real-world Python applications, searching a dictionary by value is common when dealing with data mappings, API responses, or configuration files.

For example, imagine you’re analyzing sales data from different U.S. states:

state_sales = {
    "California": 10500,
    "Texas": 8700,
    "New York": 10500,
    "Florida": 9200
}

target_sales = 10500

matching_states = [state for state, sales in state_sales.items() if sales == target_sales]

print("States with sales of $10,500:", matching_states)

Output:

States with sales of $10,500: ['California', 'New York']

This is a practical way to identify which states hit a specific sales milestone, something I often do in analytics projects.

Bonus Tip – Case-Insensitive Search in Python

If your dictionary values are strings and you want to perform a case-insensitive search, you can easily modify your Python code.

Here’s an example:

employees = {
    "EMP001": "Sales",
    "EMP002": "Marketing",
    "EMP003": "sales",
    "EMP004": "Finance"
}

# Value to search for (case-insensitive)
search_value = "sales"

# Find keys ignoring case
matching_keys = [key for key, value in employees.items() if value.lower() == search_value.lower()]

print("Employees in Sales department:", matching_keys)

Output:

Employees in Sales department: ['EMP001', 'EMP003']

This small tweak ensures your Python search logic is more robust and user-friendly.

Key Takeaways

  • Python doesn’t have a direct built-in function to search a dictionary by value, but it offers multiple simple workarounds.
  • For loops and list comprehensions are the most common and readable methods.
  • The next() function is ideal when you only need the first matching key.
  • Dictionary comprehensions help you create filtered dictionaries efficiently.
  • Always consider performance when working with large datasets.

When working with Python dictionaries, I’ve found that understanding these lookup techniques saves a lot of time and helps keep my code clean and efficient. Whether you’re analyzing sales data, managing user records, or processing API responses, these methods will come in handy.

You may also like to 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.