Open In App
Related Articles

Python program to extract key-value pairs with substring in a dictionary

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a dictionary list, extract all the dictionaries which have substring present in particular key.

Input : [{“Gfg” : “4”, “best” : “1”}, {“Gfg” : “good CS content”, “best” : “10”}], K = “Gfg”, sub_str = “CS” 
Output : [{‘Gfg’: ‘good CS content’, ‘best’: ’10’}] 
Explanation : “Gfg” has “CS” as substring value.

Input : [{“Gfg” : “4”, “best” : “1”}, {“Gfg” : “good content”, “best” : “10”}], K = “Gfg”, sub_str = “CS” 
Output : [] 
Explanation : No dictionary with “CS” as substring to “Gfg” key. 

Method #1 : Using list comprehension

This is one of the ways in which this task can be performed. In this, we iterate for all the dictionaries and check for key’s value substring presence using in operator.

Python3

# Python3 code to demonstrate working of
# Dictionaries with Substring values
# Using list comprehension
 
# initializing lists
test_list = [{"Gfg": "4", "best": "1"},
             {"Gfg": "good for CS", "best": "8"},
             {"Gfg": "good CS content", "best": "10"}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K key
K = "Gfg"
 
# initializing target value
sub_str = "CS"
 
# list comprehension to extract values with
# substring values using in operator
res = [val for val in test_list if sub_str in val[K]]
 
# printing result
print("Dictionaries with particular substring values : " + str(res))

                    

Output
The original list : [{'Gfg': '4', 'best': '1'}, {'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]
Dictionaries with particular substring values : [{'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]

Output:

The original list : [{‘Gfg’: ‘4’, ‘best’: ‘1’}, {‘Gfg’: ‘good for CS’, ‘best’: ‘8’}, {‘Gfg’: ‘good CS content’, ‘best’: ’10’}] Dictionaries with particular substring values : [{‘Gfg’: ‘good for CS’, ‘best’: ‘8’}, {‘Gfg’: ‘good CS content’, ‘best’: ’10’}]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 :  Using map() + in operator

This is yet another way in which this task can be performed. In this, we extract all the values with required substring using map() + lambda function. The in operator is used to check for substring inside the key’s value.

Python3

# Python3 code to demonstrate working of
# Dictionaries with Substring values
# Using map() + in operator
 
# initializing lists
test_list = [{"Gfg": "4", "best": "1"},
             {"Gfg": "good for CS", "best": "8"},
             {"Gfg": "good CS content", "best": "10"}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K key
K = "Gfg"
 
# initializing target value
val = "CS"
 
# map() used to perform filtering
res = list(map(lambda sub: val in sub[K], test_list))
res = [test_list[idx] for idx, ele in enumerate(res) if res[idx]]
 
# printing result
print("Dictionaries with particular substring values : " + str(res))

                    

Output
The original list : [{'Gfg': '4', 'best': '1'}, {'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]
Dictionaries with particular substring values : [{'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]

Output:

The original list : [{‘Gfg’: ‘4’, ‘best’: ‘1’}, {‘Gfg’: ‘good for CS’, ‘best’: ‘8’}, {‘Gfg’: ‘good CS content’, ‘best’: ’10’}] Dictionaries with particular substring values : [{‘Gfg’: ‘good for CS’, ‘best’: ‘8’}, {‘Gfg’: ‘good CS content’, ‘best’: ’10’}]

Method #3: Using a for loop

Use a for loop to iterate over the list of dictionaries, and a conditional statement to check if the substring exists in the value of the “K” key. 

Python3

# Python3 code to demonstrate working of
# Dictionaries with Substring values
# Using for loop
 
# initializing lists
test_list = [{"Gfg": "4", "best": "1"},
             {"Gfg": "good for CS", "best": "8"},
             {"Gfg": "good CS content", "best": "10"}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K key
K = "Gfg"
 
# initializing target value
sub_str = "CS"
 
# initialize empty list to store results
res = []
 
# iterate over each dictionary in the list
for d in test_list:
    # check if the substring exists in the value of the "K" key
    if sub_str in d[K]:
        # if yes, append the dictionary to the result list
        res.append(d)
 
# printing result
print("Dictionaries with particular substring values : " + str(res))

                    

Output
The original list : [{'Gfg': '4', 'best': '1'}, {'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]
Dictionaries with particular substring values : [{'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]

Time Complexity: O(n*k) where n is the number of dictionaries in the list and k is the length of the value of the “K” key in each dictionary.
Auxiliary Space: O(m) where m is the number of dictionaries that contain the substring in the value of the “K” key. The space used by the result list.

Method #4: Using filter() function

 step-by-step approach for the code:

  1. We start by initializing a list of dictionaries called test_list, which contains key-value pairs.
  2. We print the original list to the console using the print() function.
  3. We initialize a string variable K to “Gfg”, which is the key we want to search in the dictionaries.
  4. We initialize a string variable sub_str to “CS”, which is the substring we want to search for in the values associated with the key K.
  5. We apply the filter() function on the test_list using a lambda function that takes a dictionary x and checks if the substring sub_str exists in the value associated with the key K. If the condition is true, the lambda function returns True, and x is included in the filtered output. Otherwise, False is returned, and x is excluded from the output.
  6. We convert the filter object to a list using the list() function and store the output in a variable called res.
  7. Finally, we print the dictionaries in res that have the substring in the value associated with the key K.
     

Python3

# Python3 code to demonstrate working of
# Dictionaries with Substring values
# Using filter() function
 
# initializing lists
test_list = [{"Gfg": "4", "best": "1"},
             {"Gfg": "good for CS", "best": "8"},
             {"Gfg": "good CS content", "best": "10"}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K key
K = "Gfg"
 
# initializing target value
sub_str = "CS"
 
# apply filter function on test_list
res = list(filter(lambda x: sub_str in x[K], test_list))
 
# printing result
print("Dictionaries with particular substring values : " + str(res))

                    

Output
The original list : [{'Gfg': '4', 'best': '1'}, {'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]
Dictionaries with particular substring values : [{'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]

Time complexity: O(n), where n is the number of dictionaries in the input list.
Auxiliary space: O(1), as we are only using a constant amount of space to store the key and target substring.

Method #5 : Using find() method

Approach

  1. Initiate a for loop to traverse over list of dictionaries, initialise an empty list res
  2. Check if K key’s value has sub_str in it using find() method , if yes append the dictionary to output list res
  3. Display res

Python3

# Python3 code to demonstrate working of
# Dictionaries with Substring values
 
# initializing lists
test_list = [{"Gfg": "4", "best": "1"},
            {"Gfg": "good for CS", "best": "8"},
            {"Gfg": "good CS content", "best": "10"}]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K key
K = "Gfg"
 
# initializing target value
sub_str = "CS"
 
res = []
for i in test_list:
    if i[K].find(sub_str)!=-1:
        res.append(i)
 
# printing result
print("Dictionaries with particular substring values : " + str(res))

                    

Output
The original list : [{'Gfg': '4', 'best': '1'}, {'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]
Dictionaries with particular substring values : [{'Gfg': 'good for CS', 'best': '8'}, {'Gfg': 'good CS content', 'best': '10'}]

Time Complexity: O(n*k) where n is the number of dictionaries in the list and k is the length of the value of the “K” key in each dictionary.
Auxiliary Space: O(m) where m is the number of dictionaries that contain the substring in the value of the “K” key. The space used by the result list.



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