Python Program to Search an Element in a Dictionary

Dictionaries are used to store data values in key:value pairs. Keys are unique and dictionaries are ordered, changeable, and allow you to quickly find values using their corresponding keys.

In this article, we will explore different methods to search for elements in a dictionary. You can search by value to find the corresponding key, or check if a key exists in the dictionary.

Common Search Operations

Here are the main methods to search elements in a dictionary ?

  • Using "for" loop with "in" operator

  • Using items() with list comprehension

  • Using a custom function with items()

  • Using list.index() method

Method 1: Using "for" Loop and "in" Operator

The most straightforward approach is to iterate through the dictionary and compare values ?

fruits = {'apple': 1, 'mango': 2, 'cherry': 3}
print("The original dictionary is:", fruits)

val = 3
result_key = None

for key in fruits:
    if fruits[key] == val:
        result_key = key
        break

if result_key:
    print("The key corresponding to value", val, "is:", result_key)
else:
    print("Value not found")
The original dictionary is: {'apple': 1, 'mango': 2, 'cherry': 3}
The key corresponding to value 3 is: cherry

Method 2: Using items() with List Comprehension

This method uses list comprehension to find all keys that match a specific value ?

fruits = {'apple': 1, 'mango': 2, 'cherry': 3}
print("The original dictionary is:", fruits)

val = 2
result = [key for key, value in fruits.items() if value == val]
print("The key(s) corresponding to value", val, ":", result)
The original dictionary is: {'apple': 1, 'mango': 2, 'cherry': 3}
The key(s) corresponding to value 2 : ['mango']

Method 3: Using Custom Function with items()

Create a reusable function to search for values in any dictionary ?

def get_key(dictionary, val):
    for key, value in dictionary.items():
        if val == value:
            return key
    return "Key doesn't exist"

programs = {"Java": 100, "Python": 112, "C": 11}
print("Search for value 100:", get_key(programs, 100))
print("Search for value 11:", get_key(programs, 11))
print("Search for value 999:", get_key(programs, 999))
Search for value 100: Java
Search for value 11: C
Search for value 999: Key doesn't exist

Method 4: Using list.index()

This one-liner approach converts dictionary keys and values to lists, then finds the index ?

programs = {"Java": 100, "Python": 112, "C": 11}

try:
    key = list(programs.keys())[list(programs.values()).index(112)]
    print("Key for value 112:", key)
except ValueError:
    print("Value not found")
Key for value 112: Python

Checking if Key Exists

To check if a key exists in a dictionary, use the "in" operator ?

fruits = {'apple': 1, 'mango': 2, 'cherry': 3}

if 'apple' in fruits:
    print("'apple' exists with value:", fruits['apple'])
else:
    print("'apple' not found")

if 'banana' in fruits:
    print("'banana' exists")
else:
    print("'banana' not found")
'apple' exists with value: 1
'banana' not found

Comparison

Method Best For Returns Multiple Results?
For loop with "in" Simple search, first match No
List comprehension Multiple matches Yes
Custom function Reusable code No (first match)
list.index() One-liner solution No

Conclusion

Use list comprehension when you need all matching keys, or a simple for loop for the first match. The "in" operator is the most efficient way to check if a key exists in a dictionary.

Updated on: 2026-03-27T05:45:10+05:30

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements