Python - Remove keys with Values Greater than K ( Including mixed values )
Last Updated :
27 Jan, 2025
We are given a dictionary we need to remove the keys with values greater than K. For example, we are given a dictionary d = {'a': 1, 'b': 5, 'c': 'hello', 'd': [1, 2, 3]} we need to remove the keys with value greater than K so that output should be {'a': 1, 'c': 'hello', 'd': [1, 2, 3]}. We can use approach like dictionary comprehension, del method and multiple approaches for this.
Using Dictionary Comprehension
Dictionary comprehension allows us to filter key-value pairs based on a condition creating a new dictionary with only desired items.
Python
d = {'a': 1, 'b': 5, 'c': 'hello', 'd': [1, 2, 3]}
# Use dictionary comprehension to filter out keys with values greater than K (e.g., K = 3)
d = {key: value for key, value in d.items() if (isinstance(value, (int, float)) and value <= 3) or not isinstance(value, (int, float))}
print(d)
Output{'a': 1, 'c': 'hello', 'd': [1, 2, 3]}
Explanation:
- Dictionary comprehension iterates through each key-value pair in
d
keeping only those where the value is less than or equal to 3
(for numeric values), or the value is not numeric (like strings or lists). - Modified dictionary
{'a': 1, 'c': 'hello', 'd': [1, 2, 3]}
is printed, excluding the key 'b'
since its value (5
) is greater than 3
Using del
keyword
del keyword is used to remove a specific key-value pair from a dictionary by specifying the key. If the key exists it is deleted otherwise a KeyError is raised.
Python
d = {'a': 1, 'b': 5, 'c': 'hello', 'd': [1, 2, 3]}
# Iterate through the dictionary keys
for key in list(d.keys()): # Use list() to avoid modifying the dictionary while iterating
if isinstance(d[key], (int, float)) and d[key] > 3: # Check if value is greater than K (e.g., 3)
del d[key] # Delete the key-value pair
print(d)
Output{'a': 1, 'c': 'hello', 'd': [1, 2, 3]}
Explanation:
- Code iterates over the keys of the dictionary
d
, checking if the value associated with each key is a numeric type (int or float) and greater than 3
, and if so deletes the key-value pair. - Modified dictionary
{'a': 1, 'c': 'hello', 'd': [1, 2, 3]}
is printed, which excludes the key 'b'
as its value (5
) is greater than 3.
Using pop()
pop() method removes a key-value pair from a dictionary by specifying key and optionally returns the value. It can be used in a loop to remove keys with specific conditions such as values greater than a given threshold.
Python
d = {'a': 1, 'b': 5, 'c': 'hello', 'd': [1, 2, 3]}
# Iterate through the dictionary keys and remove keys with values greater than K (e.g., 3)
for key in list(d.keys()):
if isinstance(d[key], (int, float)) and d[key] > 3: # Check if value is greater than K
d.pop(key) # Remove the key-value pair using pop()
print(d)
Output{'a': 1, 'c': 'hello', 'd': [1, 2, 3]}
Explanation:
- Code iterates over keys of the dictionary d and checks if each value is numeric (int or float) and greater than the threshold K = 3. If so, it removes the key-value pair using the pop() method.
- Modified dictionary {'a': 1, 'c': 'hello', 'd': [1, 2, 3]} is printed, where the key 'b' is excluded because its value (5) exceeds the threshold.
Similar Reads
Python | Records with Key's value greater than K The problem of getting the suitable dictionaries that has a atleast value of the corresponding key is quite common when one starts working with dictionary. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop This is the brute force method by which this task can be
7 min read
Python - Remove Tuples with difference greater than K Given Dual Tuples List, remove pairs with differences greater than K. Input : test_list = [(4, 8), (1, 7), (9, 12), (3, 12), (2, 10)], K = 6 Output : [(4, 8), (9, 12), (1, 7)] Explanation : 4 (8 - 4), 3 (12 - 9) and 6 are all not greater than 6, hence retained. Input : test_list = [(4, 8), (1, 7), (
5 min read
Python - Remove Keys with K value We are given a dictionary we need to remove the keys with K value. For example, we are having a dictionary d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} we need to remove the keys which is having K value so that the output should be {'b': 2, 'd': 3} . We can use dictionary comprehension and many other method
3 min read
Python - Remove Dictionaries with Matching Values with K Key Given two dictionary lists, remove all the dictionaries which have similar value of K key from other dictionary list. Input : test_list = [{'Gfg' : 3, "is" : 3, "best" : 9}, {'Gfg' : 8, "is" : 4, "best" : 2}, {'Gfg' : 9, "is" : 2, "best" : 4}, {'Gfg' : 8, "is" : 10, "best" : 3}, {'Gfg' : 7, "is" : 1
5 min read
Python - Remove keys with substring values Sometimes, we need to remove keys whose values contain a specific substring. For example, consider the dictionary d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'}. If we want to remove keys where the value contains the substring 'world', the resulting dictionary should ex
2 min read