In this Python tutorial, we will study the implementation of Python Dictionary Search by value. Here we will see how to search for a value in Dictionary using some examples in python.
In Python, dictionaries are built-in data structure that stores key-value pairs. The keys in a dictionary must be unique, while the values can be of any data type.
There are a few different ways to get the key by value in a Python dictionary, depending on your specific use case.
- Using the items()method
- Using the list comprehension
- Using the index() method
- Using the filter() and lambda function
Method-1: Using the items() method
To get the key by value in a python dictionary is using the items() method and a for loop, items() method returns a view object that contains the key-value pairs of the dictionary, as tuples in a list.
# This code creates a dictionary called "countries" that contains the keys "USA", "Germany", and "France"
# and the respective values 56, 25, and 78.
countries = {'USA': 56, 'Germany': 25,'France':78}
# The for loop iterates through the items in the dictionary, where "key" represents the key of each item
# and "value" represents the value of each item.
for key, value in countries.items():
# The if statement checks if the value of each item is equal to 56.
if value == 56:
# If the value is equal to 56, the key of that item is printed.
print(key)
The above code outputs the USA as it is the key which has a value of 56.
Read: Python dictionary key error
Method 2: Using the list comprehension
This method iterates over the key-value pairs in the dictionary and uses a list comprehension to find the first key whose associated value is equal to the value you’re looking for.
# This code creates a dictionary called "my_dictionary" that contains keys and values
# representing countries and their respective values.
my_dictionary = {'Newzealand': 567,
'United Kingdom' : 456,
'China': 945,
'Japan': 845
}
# The variable new_val is set to 456
new_val = 456
# A list comprehension is used to iterate through the items in the dictionary and check if the value of each item is equal to new_val.
# If the value is equal to new_val, the key of that item is added to the list.
result=[new_k for new_k in my_dictionary.items() if new_k[1] == new_val][0][0]
# The result is printed
print(result)
The code outputs “United Kingdom” as it is the key which have value 456.
Read: Python Dictionary Count + Examples
Method-3: Using the index() method
The index() method is a built-in method in Python that is used to find the index of an element in a list. It takes a single argument, which is the element you want to find the index of and returns the index of the first occurrence of that element in the list.
# This code creates a dictionary called "my_dictionary" that contains keys and values
# representing countries and their respective values.
my_dictionary = {'Newzealand': 567,
'United Kingdom' : 456,
'China': 945,
'Japan': 845
}
# Get the values from the dictionary as a list
values = list(my_dictionary.values())
# The variable new_val is set to 456
new_val = 456
# Find the index of the value in the list
index = values.index(new_val)
# Get the key from the dictionary using the index
result = list(my_dictionary.keys())[index]
# The result is printed
print(result)
The above code outputs “United Kingdom” as it is the key which has value 456 but uses the index() method of the list.
Read: Python Dictionary duplicate keys
Method-4: Using the filter() and lambda function
The lambda function is a small anonymous function that can be used to perform a certain operation, and the filter() function is used to filter a sequence of elements based on a certain condition.
# my_dictionary is a dictionary that contains keys as countries and values as their population
my_dictionary = {'Newzealand': 567,
'United Kingdom' : 456,
'China': 945,
'Japan': 845
}
# new_val is the population value for which we want to find the corresponding country
new_val = 456
# Using lambda and filter() functions to filter the items of the dictionary
# and only the items with the value of new_val are returned
result = list(filter(lambda x: x[1] == new_val, my_dictionary.items()))[0][0]
# The result is printed
print(result)
The above code outputs “United Kingdom” as it is the key that has value 456 but uses the filter() and lambda function.
You may also like to read the following Python tutorials.
- Python Dictionary of sets
- Python dictionary contains + examples
- Python convert dictionary to list
- Python dictionary filter + Examples
In this Python tutorial, we have covered how to get a key using the value of the dictionary by following the below methods.
- Python Dictionary Search by value using the items() method
- Python Dictionary Search by value using the list comprehension
- Python Dictionary Search by value using the index() method
- Python Dictionary Search by value using the filter() and lambda function
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.