Open In App

Python – Filter dictionary values in heterogeneous dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to filter out certain values based on certain conditions on a particular type, e.g all values smaller than K. This task becomes complex when dictionary values can be heterogeneous. This kind of problem can have applications across many domains. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘Gfg’ : 10, ‘for’ : ‘geeks’} 
Output : {‘Gfg’: 10, ‘for’: ‘geeks’}



Input : test_dict = {‘Gfg’ : ‘geeks’} 
Output : {‘Gfg’: ‘geeks’} 

Method #1 : Using type() + dictionary comprehension 
The combination of above functions can be used to perform this task. In this, we check for integral type using type() and filter the data in dictionary comprehension. 



Step-by-step approach

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Filter dictionary values in heterogeneous dictionary
# Using type() + dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 3, 'for' : 'geeks'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 3
 
# Filter dictionary values in heterogeneous dictionary
# Using type() + dictionary comprehension
res = {key : val for key, val in test_dict.items()
                   if type(val) != int or val > K}
 
# printing result
print("Values greater than K : " + str(res))

Output : 
The original dictionary : {'Gfg': 4, 'for': 'geeks', 'is': 2, 'best': 3}
Values greater than K : {'Gfg': 4, 'for': 'geeks'}

 

Time Complexity: O(n)
Auxiliary Space: O(n)

 Method #2 : Using isinstance() + dictionary comprehension 
The combination of above functions can also be used to solve this problem. In this, we perform this task similar to above, but the difference being that type test is done by isinstance() rather than type().




# Python3 code to demonstrate working of
# Filter dictionary values in heterogeneous dictionary
# Using isinstance() + dictionary comprehension
 
# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 3, 'for' : 'geeks'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 3
 
# Filter dictionary values in heterogeneous dictionary
# Using isinstance() + dictionary comprehension
res = {key : val for key, val in test_dict.items()
           if not isinstance(val, int) or val > K}
 
# printing result
print("Values greater than K : " + str(res))

Output : 
The original dictionary : {'Gfg': 4, 'for': 'geeks', 'is': 2, 'best': 3}
Values greater than K : {'Gfg': 4, 'for': 'geeks'}

 

Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(k), where k is the number of items in the resulting dictionary after filtering.

Method 3: Using a for loop and conditional statements:

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Filter dictionary values in heterogeneous dictionary
# Using for loop and conditional statements
 
# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 3, 'for' : 'geeks'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 3
 
# Filter dictionary values in heterogeneous dictionary
# Using for loop and conditional statements
res = {}
for key, val in test_dict.items():
    if type(val) != int or val > K:
        res[key] = val
 
# printing result
print("Values greater than K : " + str(res))

Output
The original dictionary : {'Gfg': 4, 'is': 2, 'best': 3, 'for': 'geeks'}
Values greater than K : {'Gfg': 4, 'for': 'geeks'}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.

Method #4: Using a dictionary comprehension with if condition:

Step-by-step approach:

Below is the implementation of the above approach:




# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 3, 'for' : 'geeks'}
 
# initializing K
K = 3
 
# Filter dictionary values in heterogeneous dictionary
# Using dictionary comprehension with if condition
res = {k:v for k, v in test_dict.items() if type(v) != int or v > K}
 
# printing result
print("Values greater than K : " + str(res))

Output
Values greater than K : {'Gfg': 4, 'for': 'geeks'}

 Time complexity: O(n) as it loops through all the items in the dictionary once. 
Auxiliary space: O(n) as it creates a new dictionary to store the filtered items.

Method #6: Using filter() function with lambda function

Step-by-step approach:

Below is the implementation of the above approach:




# initializing dictionary
test_dict = {'Gfg' : 4, 'is' : 2, 'best' : 3, 'for' : 'geeks'}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing K
K = 3
 
# Filter dictionary values in heterogeneous dictionary
# Using filter() function with lambda function
res = dict(filter(lambda item: not(isinstance(item[1], int) and item[1] <= K), test_dict.items()))
 
# printing result
print("Values greater than K : " + str(res))

Output
The original dictionary : {'Gfg': 4, 'is': 2, 'best': 3, 'for': 'geeks'}
Values greater than K : {'Gfg': 4, 'for': 'geeks'}

Time Complexity: O(n), where n is the size of the input dictionary
Auxiliary Space: O(n), where n is the size of the input dictionary. This is the space required to store the filtered dictionary.


Article Tags :