Python – Remove keys with Values Greater than K ( Including mixed values )
Last Updated :
06 Apr, 2023
Given a dictionary with key-value pairs, remove all the keys with values greater than K, including mixed values.
Input : test_dict = {‘Gfg’ : 3, ‘is’ : 7, ‘best’ : 10, ‘for’ : 6, ‘geeks’ : ‘CS’}, K = 7
Output : {‘Gfg’ : 3, ‘for’ : 6, ‘geeks’ : ‘CS’}
Explanation : All values greater than K are removed. Mixed value is retained.
Input : test_dict = {‘Gfg’ : 3, ‘is’ : 7, ‘best’ : 10, ‘for’ : 6, ‘geeks’ : ‘CS’}, K = 1
Output : {‘geeks’ : ‘CS’}
Explanation : Only Mixed value is retained.
Method #1: Using isinstance() + loop
This is one of the ways in which this task can be performed. In this, we use instance to get integral values and comparisons to get values not greater than K.
Python3
test_dict = { 'Gfg' : 3 , 'is' : 7 , 'best' : 10 , 'for' : 6 , 'geeks' : 'CS' }
print ( "The original dictionary is : " + str (test_dict))
K = 6
res = {}
for key in test_dict:
if not ( isinstance (test_dict[key], int ) and test_dict[key] > K):
res[key] = test_dict[key]
print ( "The constructed dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'geeks': 'CS'}
The constructed dictionary : {'Gfg': 3, 'for': 6, 'geeks': 'CS'}
Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(m), where m is the number of keys that satisfy the condition (values greater than K and of type int).
Method #2 : Using dictionary comprehension + isinstance()
The combination of above functions is used to solve this problem. This performs task similar to above method. The only difference being that it performs in one liner using dictionary comprehension.
Python3
test_dict = { 'Gfg' : 3 , 'is' : 7 , 'best' : 10 , 'for' : 6 , 'geeks' : 'CS' }
print ( "The original dictionary is : " + str (test_dict))
K = 6
res = {key : val for key, val in test_dict.items() if not ( isinstance (val, int ) and (val > K))}
print ( "The constructed dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'geeks': 'CS'}
The constructed dictionary : {'Gfg': 3, 'for': 6, 'geeks': 'CS'}
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n).
Method #3: Using dictionary comprehension and filter()
We can also solve this problem using a combination of dictionary comprehension and the built-in function filter().
Step-by-step approach:
- Initialize the original dictionary test_dict.
- Print the original dictionary.
- Initialize the variable K with the value 6.
- Use the filter() function to remove keys with values greater than K. To do this, we create a lambda function that takes a key-value pair as input and returns True if the value is not an integer or if it is an integer but is less than or equal to K. We then pass this lambda function to filter() along with the items of test_dict using the items() method. Finally, we convert the filtered items back to a dictionary using the dict() constructor and store it in the variable res.
- Print the constructed dictionary.
Below is the implementation of the above approach:
Python3
test_dict = { 'Gfg' : 3 , 'is' : 7 , 'best' : 10 , 'for' : 6 , 'geeks' : 'CS' }
print ( "The original dictionary is: " + str (test_dict))
K = 6
res = dict ( filter ( lambda item: not ( isinstance (item[ 1 ], int ) and item[ 1 ] > K), test_dict.items()))
print ( "The constructed dictionary: " + str (res))
|
Output
The original dictionary is: {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'geeks': 'CS'}
The constructed dictionary: {'Gfg': 3, 'for': 6, 'geeks': 'CS'}
Time complexity: O(n), where n is the number of key-value pairs in the dictionary. This is because we need to iterate over each key-value pair in the dictionary once.
Auxiliary space: O(m), where m is the number of key-value pairs in the new dictionary. This is because we need to create a new dictionary to store the filtered key-value pairs.
Please Login to comment...