Open In App

Python – Remove Keys with K value

Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionary, remove all keys with value equals K.

Input : test_dict = {‘Gfg’ : 8, ‘is’ : 7, ‘best’ : 8, ‘for’ : 6, ‘geeks’ : 11}, K = 8 Output : {‘is’ : 7, ‘for’ : 6, ‘geeks’ : 11} Explanation : “Gfg” and “Best”, valued 8, are removed. Input : test_dict = {‘Gfg’ : 8, ‘is’ : 8, ‘best’ : 8, ‘for’ : 8, ‘geeks’ : 8}, K = 8 Output : {} Explanation : All keys, valued 8, are removed.

Method #1 : Using dictionary comprehension

This is one of the ways in which this task can be performed. In this, we check for only elements that are not equal to K and retain it, inside dictionary comprehension as one-liner.

Python3




# Python3 code to demonstrate working of
# Remove Keys with K value
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 6, 'is' : 7, 'best' : 9, 'for' : 6, 'geeks' : 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 6
 
# using dictionary comprehension
# to compare not equal to K and retain
res = {key: val for key, val in test_dict.items() if val != K}
         
# printing result
print("The filtered dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 6, 'geeks': 11}
The filtered dictionary : {'is': 7, 'best': 9, 'geeks': 11}

Method #2 : Using dict() + filter() + lambda

The combination of above functions can be used to solve this problem. In this, we filter all the non-K elements and retain. Finally the result is converted to dictionary using dict().

Python3




# Python3 code to demonstrate working of
# Remove Keys with K value
# Using dict() + filter() + lambda
 
# initializing dictionary
test_dict = {'Gfg' : 6, 'is' : 7, 'best' : 9, 'for' : 6, 'geeks' : 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 6
 
# employing lambda for computation
# filter() to perform filter according to lambda
res = dict(filter(lambda key: key[1] != K, test_dict.items()))
         
# printing result
print("The filtered dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 6, 'is': 7, 'best': 9, 'for': 6, 'geeks': 11}
The filtered dictionary : {'is': 7, 'best': 9, 'geeks': 11}

Method#3:Using for loop with del keyword

Approach

this approach involves using a for loop to iterate through each key-value pair in the input dictionary ‘test_dict’. If the value of the current key-value pair is equal to the given value ‘K’, the key-value pair is deleted from ‘test_dict’ using the ‘del’ keyword. Finally, the modified ‘test_dict’ is returned. 

Algorithm

1. Iterate through each key-value pair in the input dictionary ‘test_dict’.
2. If the value of the current key-value pair is equal to the given value ‘K’, delete the key-value pair from ‘test_dict’.
3. Return the modified ‘test_dict’.

Python3




def remove_keys(test_dict, K):
    for key, val in list(test_dict.items()):
        if val == K:
            del test_dict[key]
    return test_dict
 
test_dict = {'Gfg' : 6, 'is' : 7, 'best' : 9, 'for' : 6, 'geeks' : 11}
K = 6
print(remove_keys(test_dict, K))


Output

{'is': 7, 'best': 9, 'geeks': 11}

Time Complexity: O(N), where N is the number of key-value pairs in the input dictionary.
Space Complexity: O(1), as we are modifying the input dictionary in-place. However, we are creating a list of key-value pairs using ‘list()’ method which takes additional space.

Approach#4: Using reduce + lambda

The reduce() function is used to iterate through the keys of the input dictionary. A lambda function is passed to reduce(), which takes two arguments, the first being the result dictionary, and the second being the current key of the input dictionary. The lambda function checks whether the value of the current key is equal to the given K value. If the value is equal to K, the lambda function returns the result dictionary unchanged. Otherwise, the lambda function updates the result dictionary with the current key-value pair and returns the updated result dictionary.

Algorithm

1. Import the reduce() function from the functools module.
2. Initialize the input dictionary and K value.
3. Define a lambda function that takes two arguments, the result dictionary and the current key of the input dictionary.
4. Use the reduce() function to iterate through the keys of the input dictionary and apply the lambda function to each key.
5. Print the updated result dictionary.

Python3




from functools import reduce
 
test_dict = {'Gfg': 8, 'is': 7, 'best': 8, 'for': 6, 'geeks': 11}
K = 8
 
result_dict = reduce(lambda res, key: res if test_dict[key] == K else (res.update({key: test_dict[key]})) or res, test_dict.keys(), {})
 
print(result_dict)


Output

{'is': 7, 'for': 6, 'geeks': 11}

Time Complexity: O(n) , The time complexity of the reduce() function is O(n), where n is the number of keys in the input dictionary. The lambda function has a constant time complexity of O(1) as it performs a single dictionary lookup and update operation. 

Auxiliary Space:  O(n), as we are creating a new dictionary to store the filtered key-value pairs. The size of the new dictionary can be at most equal to the size of the original dictionary. Therefore, the space complexity is linear with respect to the input size.



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