Python – Check for Key in Dictionary Value list

Sometimes, while working with data, we might have a problem we receive a dictionary whole key has list of dictionaries as value. In this scenario, we might need to find if a particular key exists in that. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using any()
This is simple and most recommended way in which this task can be performed. In this, we just check for the key inside the values by iteration.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Check for Key in Dictionary Value list
# Using any()
    
# initializing dictionary 
test_dict = {'Gfg' : [{'CS' : 5}, {'GATE' : 6}], 'for' : 2, 'CS' : 3
    
# printing original dictionary 
print("The original dictionary is : " + str(test_dict)) 
    
# initializing key
key = "GATE"
    
# Check for Key in Dictionary Value list
# Using any()
res = any(key in ele for ele in test_dict['Gfg'])
    
# printing result  
print("Is key present in nested dictionary list ?  : " + str(res)) 

chevron_right


Output :

The original dictionary is : {'Gfg': [{'CS': 5}, {'GATE': 6}], 'for': 2, 'CS': 3}
Is key present in nested dictionary list ?  : True

 

Method #2 : Using list comprehension + in operator
The combination of above functionalities can be used to perform this task. In this, we iterate through the list using comprehension and perform key flattening and store keys. Then we check for desired key using in operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Check for Key in Dictionary Value list
# Using list comprehension + in operator
    
# initializing dictionary 
test_dict = {'Gfg' : [{'CS' : 5}, {'GATE' : 6}], 'for' : 2, 'CS' : 3
    
# printing original dictionary 
print("The original dictionary is : " + str(test_dict)) 
    
# initializing key
key = "GATE"
    
# Check for Key in Dictionary Value list
# Using list comprehension + in operator
res = key in [sub for ele in test_dict['Gfg'] for sub in ele.keys()]
    
# printing result  
print("Is key present in nested dictionary list ?  : " + str(res)) 

chevron_right


Output :

The original dictionary is : {'Gfg': [{'CS': 5}, {'GATE': 6}], 'for': 2, 'CS': 3}
Is key present in nested dictionary list ?  : True



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.