Python – Filter dictionary values in heterogenous dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to filter out certain values based on certain condition on a particular type, e.g all values smaller than K. This task becomes complex when dictionary values can be heterogenous. 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Filter dictionary values in heterogenous 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 heterogenous 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)) 

chevron_right


Output :



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

 

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 difference being that type test is done by isintance() rather than type().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Filter dictionary values in heterogenous 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 heterogenous 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)) 

chevron_right


Output :

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.