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 - Extract Key's Value, if Key Present in List and Dictionary
- Python program to extract key-value pairs with substring in a dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python - Extract target key from other key values
- Python - Filter key's value from other key
- Python - Extracting Key from Value Substring
- Python | Extract key-value of dictionary in variables
- How to use a List as a key of a Dictionary in Python 3?
- Python | Ways to remove a key from dictionary
- Python | Check whether given key already exists in a dictionary
- Python | Accessing Key-value in Dictionary
- Python | Tuple key dictionary conversion
- Python | Get key from value in Dictionary
- Add a key:value pair to dictionary in Python
- Python | Remove item from dictionary when key is unknown
- Python | Get key with maximum value in Dictionary
- Python | Convert byteString key:value pair of dictionary to String
- Python | Sum values for each key in nested dictionary
- Python | Filter dictionary key based on the values in selective list
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.

