Open In App

Python | Search Key from Value

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Search Key from Value
# Using naive method
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing value
val = 3
 
# Using naive method
# Search key from Value
for key in test_dict:
    if test_dict[key] == val:
        res = key
 
# printing result
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




# Python3 code to demonstrate working of
# Search Key from Value
# Using items() + list comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing value
val = 3
 
# Using items() + list comprehension
# Search key from Value
res = [key for key, value in test_dict.items() if value == val]
 
# printing result
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




# Python3 code to demonstrate working of
# Search Key from Value
# Using reduce() + lambda
 
# importing functools for reduce()
import functools
 
# initializing dictionary
test_dict = {'Gfg': 1, 'for': 2, 'CS': 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing value
val = 3
 
# Using reduce() + lambda
# Search key from Value
res = functools.reduce(lambda k, v: k if test_dict[k] == val else v, test_dict)
 
# printing result
print("The key corresponding to value : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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 

  1. Initialize an empty list called res.
  2. Use a for loop to iterate through the key-value pairs in the test_dict dictionary using the items() method.
  3. Check if the value of the current key-value pair is equal to the value of val.
  4. If the value of the current key-value pair matches val, append the key to the res list using the append() method.
  5. Print the result using the print() function and str() function to convert the list to a string.

Python3




# Python3 code to demonstrate working of
# Search Key from Value
# Using for loop
 
# initializing dictionary
test_dict = {'Gfg' : 1, 'for' : 2, 'CS' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing value
val = 3
 
# Using for loop
# Search key from Value
res = []
for key, value in test_dict.items():
    if value == val:
        res.append(key)
 
# printing result
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.



Last Updated : 15 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads