Python – Extracting Key from Value Substring
Last Updated :
11 Apr, 2023
Sometimes, while working with Python dictionaries, we can have a problem in which we need to find the key from given value, querying substring from key’s value. This kind of problem is common and have application in many domains including web development. Lets discuss certain ways in which this task can be performed.
Input : test_dict = {1 : ‘Gfg is best’, 2 : ‘CS is best’} Output : [1, 2] Input : test_dict = {1 : ‘best’} Output : [1]
Method #1 : Using loop + items() The combination of above functionalities, can be used to solve this problem. In this, we extract the dictionary values using items() and loop is used to check for substring using “in” operator.
Python3
test_dict = { 1 : 'Gfg is good' , 2 : 'Gfg is best' , 3 : 'Gfg is on top' }
print ("The original dictionary : " + str (test_dict))
srch_wrd = 'best'
res = []
for key, val in test_dict.items():
if srch_wrd in val:
res.append(key)
print ("The Corresponding key : " + str (res))
|
Output :
The original dictionary : {1: ‘Gfg is good’, 2: ‘Gfg is best’, 3: ‘Gfg is on top’} The Corresponding key : [2]
Method #2 : Using list comprehension This is yet another way in which this task can be performed. In this, we perform the above method in one liner in compact way.
Python3
test_dict = { 1 : 'Gfg is good' , 2 : 'Gfg is best' , 3 : 'Gfg is on top' }
print ("The original dictionary : " + str (test_dict))
srch_wrd = 'best'
res = [key for key, val in test_dict.items() if srch_wrd in val]
print ("The Corresponding key : " + str (res))
|
Output :
The original dictionary : {1: ‘Gfg is good’, 2: ‘Gfg is best’, 3: ‘Gfg is on top’} The Corresponding key : [2]
Method #3: Using filter+lambda
Approach
The given problem can be solved by iterating through each key-value pair of the dictionary and checking if the substring srch_wrd is present in the value of each key. If the substring is present, then the corresponding key is appended to the output list. Alternatively, the filter() function can be used to filter the keys of the dictionary based on the condition that the substring srch_wrd is present in the value of each key.
step-by-step Algorithm
1.define a substring
2.define input dictionary
3.use filter and lambda to get the filtered keys
4. make a list with the filtered keys
5. finally print the output
Python3
srch_wrd = 'best'
test_dict = { 1 : 'Gfg is best' , 2 : 'CS is best' }
filtered_keys = filter ( lambda key: srch_wrd in test_dict[key], test_dict.keys())
output = list (filtered_keys)
print (output)
|
Time Complexity: O(N), N is the number of key-value pairs in the dictionary. This is because filter() function is an O(N) operation.
Space complexity: O(K), where K is the number of keys that have a value containing the substring srch_wrd.
METHOD 4:Using def function
APPROACH:
The extract_keys_5 function takes a dictionary and a substring as input and returns a list of keys from the dictionary whose corresponding value contains the substring. The function loops through each key-value pair in the dictionary and checks if the substring is present in the value using the in keyword. If the substring is found in the value, the key is appended to the result list. Finally, the function returns the result list
ALGORITHM:
1. Start
2. Define a function extract_keys_5(dictionary, substring)
3. Create an empty list result = []
4. For each key-value pair in the dictionary using items() method
a. Check if substring is present in the value of each key-value pair using the in operator
b. If the substring is present, append the corresponding key to the result list
5. Return the result list
6. End
Python3
def extract_keys_5(dictionary, substring):
result = []
for k, v in dictionary.items():
if substring in v:
result.append(k)
return result
dictionary = { 1 : 'Gfg is good' , 2 : 'Gfg is best' }
substring = 'Gfg is'
result = extract_keys_5(dictionary, substring)
print (result)
|
Time Complexity: O(nm), where n is the number of key-value pairs in the dictionary and m is the length of the substring.
Auxiliary Space: O(k), where k is the number of matching keys.
Share your thoughts in the comments
Please Login to comment...