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.
# 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)) |
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.
# 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)) |
The original dictionary is : {'have': 2, 'good': 3, 'food': 4, 'All': 1} Key-Value pair for substring keys : {'good': 3, 'food': 4}
Recommended Posts:
- Python | Prefix key match in dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | re.search() vs re.match()
- Python | Get match indices
- Python | Pandas Series.str.match()
- Regular Expressions in Python | Set 2 (Search, Match and Find All)
- Python | Get the first key in dictionary
- Python Dictionary
- Python | Get key from value in Dictionary
- Add a key:value pair to dictionary in Python
- Python | Check if key has Non-None value in dictionary
- Python | Add new keys to a dictionary
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.