Python | Substring Key match in dictionary

Sometimes, while working with dictionaries, we might have a use case in which we are not known the exact keys we require but just a specific part of keys that we require to fetch. This kind of problem can arise in many applications. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using items() + list comprehension
The combination of above method can be used to perform this particular task in which we just access the key value pairs using the items function and list comprehension helps in the iteration and access logic.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Substring Key match in dictionary
# Using items() + list comprehension
  
# initializing dictionary
test_dict = {'All' : 1, 'have' : 2, 'good' : 3, 'food' : 4}
  
# initializing search key string
search_key = 'ood'
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Using items() + list comprehension
# Substring Key match in dictionary
res = [val for key, val in test_dict.items() if search_key in key]
  
# printing result 
print("Values for substring keys : " + str(res))

chevron_right


Output :



The original dictionary is : {'All': 1, 'food': 4, 'have': 2, 'good': 3}
Values for substring keys : [4, 3]

 

Method #2 : Using dict() + filter() + lambda
The combination of above functions can be used to perform this particular task. In this, the dict and filter function is used to convert the result to dictionary and query for the substring in list respectively. The lambda performs the task of accessing all key-value pairs.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Substring Key match in dictionary
# Using dict() + filter() + lambda
  
# initializing dictionary
test_dict = {'All' : 1, 'have' : 2, 'good' : 3, 'food' : 4}
  
# initializing search key string
search_key = 'ood'
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Using dict() + filter() + lambda
# Substring Key match in dictionary
res = dict(filter(lambda item: search_key in item[0], test_dict.items()))
  
# printing result 
print("Key-Value pair for substring keys : " + str(res))

chevron_right


Output :

The original dictionary is : {'have': 2, 'good': 3, 'food': 4, 'All': 1}
Key-Value pair for substring keys : {'good': 3, 'food': 4}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.