Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Remove keys with Values Greater than K ( Including mixed...

Python – Remove keys with Values Greater than K ( Including mixed values )

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, ‘Lazyroar’ : ‘CS’}, K = 7 
Output : {‘Gfg’ : 3, ‘for’ : 6, ‘Lazyroar’ : ‘CS’} 
Explanation : All values greater than K are removed. Mixed value is retained. 

Input : test_dict = {‘Gfg’ : 3, ‘is’ : 7, ‘best’ : 10, ‘for’ : 6, ‘Lazyroar’ : ‘CS’}, K = 1 
Output : {‘Lazyroar’ : ‘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




# Python3 code to demonstrate working of
# Remove keys with Values Greater than K ( Including mixed values )
# Using loop + isinstance()
 
# initializing dictionary
test_dict = {'Gfg' : 3, 'is' : 7, 'best' : 10, 'for' : 6, 'Lazyroar' : 'CS'}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 6
 
# using loop to iterate keys of dictionary
res = {}
for key in test_dict:
   
    # testing for data type and then condition, order is imp.
    if not (isinstance(test_dict[key], int) and test_dict[key] > K):
        res[key] = test_dict[key]
         
# printing result
print("The constructed dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'Lazyroar': 'CS'}
The constructed dictionary : {'Gfg': 3, 'for': 6, 'Lazyroar': '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




# Python3 code to demonstrate working of
# Remove keys with Values Greater than K ( Including mixed values )
# Using dictionary comprehension + isinstance()
 
# initializing dictionary
test_dict = {'Gfg' : 3, 'is' : 7, 'best' : 10, 'for' : 6, 'Lazyroar' : 'CS'}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 6
 
# using list comprehension to perform in one line
res = {key : val for key, val in test_dict.items() if not (isinstance(val, int) and (val > K))}
         
# printing result
print("The constructed dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'Lazyroar': 'CS'}
The constructed dictionary : {'Gfg': 3, 'for': 6, 'Lazyroar': '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




# initializing dictionary
test_dict = {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'Lazyroar': 'CS'}
 
# printing original dictionary
print("The original dictionary is: " + str(test_dict))
 
# initializing K
K = 6
 
# using filter() and dictionary comprehension to construct a new dictionary
res = dict(filter(lambda item: not (isinstance(item[1], int) and item[1] > K), test_dict.items()))
 
# printing result
print("The constructed dictionary: " + str(res))


Output

The original dictionary is: {'Gfg': 3, 'is': 7, 'best': 10, 'for': 6, 'Lazyroar': 'CS'}
The constructed dictionary: {'Gfg': 3, 'for': 6, 'Lazyroar': '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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments