Python – Remove keys with substring values
Last Updated :
25 Apr, 2023
Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove keys whose values have substring as argument we pass. This problem can occur in cases of web development and day-day programming. Lets discuss certain ways in which this task can be performed.
Input :
test_dict = {1 : ‘Gfg is best for geeks’}
sub_list = [‘love’, ‘good’] ( Strings to check in values )
Output : {1: ‘Gfg is best for geeks’}
Input :
test_dict = {1 : ‘Gfg is love’, 2: ‘Gfg is good’}
sub_list = [‘love’, ‘good’] ( Strings to check in values )
Output : {}
Method #1 : Using any() + loop
The combination of above functionalities can be used to solve this problem. In this, we extract all the items from dictionary which do not have desired values, the filtration is performed using any() and generator expression.
Python3
test_dict = { 1 : 'Gfg is best for geeks' , 2 : 'Gfg is good' , 3 : 'I love Gfg' }
print ("The original dictionary : " + str (test_dict))
sub_list = [ 'love' , 'good' ]
res = dict ()
for key, val in test_dict.items():
if not any (ele in val for ele in sub_list):
res[key] = val
print ("Filtered Dictionary : " + str (res))
|
Output :
The original dictionary: {1: ‘Gfg is best for geeks’, 2: ‘Gfg is good’, 3: ‘I love Gfg’}
Filtered Dictionary: {1: ‘Gfg is best for geeks’}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using dictionary comprehension + any()
The combination of above methods provide the shorthand to execute this task. In this, we perform this task in similar way as above method, but in one liner format using comprehension.
Python3
test_dict = { 1 : 'Gfg is best for geeks' , 2 : 'Gfg is good' , 3 : 'I love Gfg' }
print ("The original dictionary : " + str (test_dict))
sub_list = [ 'love' , 'good' ]
res = {key : val for key, val in test_dict.items() if not any (ele in val for ele in sub_list)}
print ("Filtered Dictionary : " + str (res))
|
Output :
The original dictionary : {1: 'Gfg is best for geeks', 2: 'Gfg is good', 3: 'I love Gfg'}
Filtered Dictionary : {1: 'Gfg is best for geeks'}
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using pop method
Approach
the pop method to remove the key-value pairs containing the substring from the original dictionary. Here’s the code for the same
Algorithm
1. Iterate over the key-value pairs in the ‘test_dict’.
2. For each value in the ‘test_dict’, iterate over the ‘sub_list’.
3. If the substring is found in the value, remove that key-value pair from the ‘test_dict’.
4. Return the ‘test_dict’ after removing the required key-value pairs.
Python3
test_dict = { 1 : 'Gfg is love' , 2 : 'Gfg is good' }
sub_list = [ 'love' , 'good' ]
for key, value in list (test_dict.items()):
for sub in sub_list:
if sub in value:
test_dict.pop(key)
print (test_dict)
|
Time complexity: O(n*m), where n is the number of key-value pairs in the dictionary and m is the length of the longest value string.
Auxiliary Space: O(1), as we are modifying the original dictionary.
Method 4: Using filter() + lambda
Step-by-step approach:
- Initializing dictionary
- printing original dictionary
- Initializing substrings
- Remove keys with substring values Using filter() + lambda
- Printing result
Below is the implementation of the above approach:
Python3
test_dict = { 1 : 'Gfg is best for geeks' , 2 : 'Gfg is good' , 3 : 'I love Gfg' }
print ( "The original dictionary : " + str (test_dict))
sub_list = [ 'love' , 'good' ]
res = dict ( filter ( lambda item: not any (sub in item[ 1 ] for sub in sub_list), test_dict.items()))
print ( "Filtered Dictionary : " + str (res))
|
Output
The original dictionary : {1: 'Gfg is best for geeks', 2: 'Gfg is good', 3: 'I love Gfg'}
Filtered Dictionary : {1: 'Gfg is best for geeks'}
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), as we are creating a new dictionary to store the filtered key-value pairs.
Method 5: Using Reduce():
Algorithm:
- Import the reduce function from functools module.
- Initialize a dictionary called test_dict.
- Print the original dictionary.
- Initialize the sub_list which contains the substrings to be removed.
- Using the filter() function with lambda function, remove the keys with substring values from the dictionary.
Using the reduce() function with lambda function, filter the dictionary keys that don’t have the substring values and create a new dictionary with only those keys.
Print the filtered dictionary.
Python3
from functools import reduce
test_dict = { 1 : 'Gfg is best for geeks' , 2 : 'Gfg is good' , 3 : 'I love Gfg' }
print ( "The original dictionary : " + str (test_dict))
sub_list = [ 'love' , 'good' ]
res = reduce ( lambda d, k: { * * d, k: test_dict[k]}, filter ( lambda k: not any (sub in test_dict[k] for sub in sub_list), test_dict), {})
print ( "Filtered Dictionary : " + str (res))
|
Output
The original dictionary : {1: 'Gfg is best for geeks', 2: 'Gfg is good', 3: 'I love Gfg'}
Filtered Dictionary : {1: 'Gfg is best for geeks'}
Time complexity:
The time complexity of the filter() function is O(n), where n is the number of elements in the dictionary. The time complexity of the reduce() function is O(n), where n is the number of elements in the dictionary. Therefore, the overall time complexity of the code is O(n).
Auxiliary Space:
The space complexity of the code is O(n), where n is the number of elements in the dictionary. This is because we are creating a new dictionary to store the filtered keys, and the size of the dictionary is proportional to the number of elements in the original dictionary.
Please Login to comment...