Python | Search Key from Value
Last Updated :
15 May, 2023
The problem of finding a value from a given key is quite common. But we may have a problem in which we wish to get the back key from the input key we feed. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using Naive Method
In this method, we just run a loop for each of the values and return the corresponding key or keys whose value match. This is the brute force way to perform this particular task.
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : 3 }
print ( "The original dictionary is : " + str (test_dict))
val = 3
for key in test_dict:
if test_dict[key] = = val:
res = key
print ( "The key corresponding to value : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': 3}
The key corresponding to value : CS
Method #2 : Using items() + list comprehension
This problem can be easily solved using the items(), which is used to extract both keys and values at once, hence making the search easy and can be executed using list comprehension making it a one liner.
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : 3 }
print ( "The original dictionary is : " + str (test_dict))
val = 3
res = [key for key, value in test_dict.items() if value = = val]
print ( "The key corresponding to value : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': 3}
The key corresponding to value : ['CS']
Method #3 : Using reduce() + lambda
Python reduce() is a function for performing some computation on a list and returning the result. This function is defined in “functools” module.
Python3
import functools
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : 3 }
print ( "The original dictionary is : " + str (test_dict))
val = 3
res = functools. reduce ( lambda k, v: k if test_dict[k] = = val else v, test_dict)
print ( "The key corresponding to value : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': 3}
The key corresponding to value : CS
Time complexity: O(n)
Auxiliary space: O(1)
Method 4: Using a for loop
- Initialize an empty list called res.
- Use a for loop to iterate through the key-value pairs in the test_dict dictionary using the items() method.
- Check if the value of the current key-value pair is equal to the value of val.
- If the value of the current key-value pair matches val, append the key to the res list using the append() method.
- Print the result using the print() function and str() function to convert the list to a string.
Python3
test_dict = { 'Gfg' : 1 , 'for' : 2 , 'CS' : 3 }
print ( "The original dictionary is : " + str (test_dict))
val = 3
res = []
for key, value in test_dict.items():
if value = = val:
res.append(key)
print ( "The key corresponding to value : " + str (res))
|
Output
The original dictionary is : {'Gfg': 1, 'for': 2, 'CS': 3}
The key corresponding to value : ['CS']
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(k), where k is the number of keys that match the given value.
Share your thoughts in the comments
Please Login to comment...