How to use filter() + lambda In Python
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
# Python3 code to demonstrate working of # Remove keys with substring values # Using filter() + lambda # initializing dictionary test_dict = { 1 : 'Gfg is best for geeks' , 2 : 'Gfg is good' , 3 : 'I love Gfg' } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # initializing substrings sub_list = [ 'love' , 'good' ] # Remove keys with substring values # Using filter() + lambda res = dict ( filter ( lambda item: not any (sub in item[ 1 ] for sub in sub_list), test_dict.items())) # printing result print ( "Filtered Dictionary : " + str (res)) |
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
# Import reduce from functools from functools import reduce # initializing dictionary test_dict = { 1 : 'Gfg is best for geeks' , 2 : 'Gfg is good' , 3 : 'I love Gfg' } # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # initializing substrings sub_list = [ 'love' , 'good' ] # Remove keys with substring values # Using reduce() + lambda 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), {}) # printing result print ( "Filtered Dictionary : " + str (res)) #This code is contributed by Jyothi pinjala. |
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.
Python – Remove keys with substring values
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 : {}
Contact Us