Python – Extract Key’s value from Mixed Dictionaries List

Given a list of dictionaries, with each dictionary having different keys, extract value of key K.

Input : test_list = [{“Gfg” : 3, “b” : 7}, {“is” : 5, ‘a’ : 10}, {“Best” : 9, ‘c’ : 11}], K = ‘b’
Output : 7
Explanation : Value of b is 7.

Input : test_list = [{“Gfg” : 3, “b” : 7}, {“is” : 5, ‘a’ : 10}, {“Best” : 9, ‘c’ : 11}], K = ‘c’
Output : 11
Explanation : Value of c is 11.

Method #1 : Using list comprehension

This is one of the ways in which this task can be performed. In this, we iterate for each dictionary inside list, and check for key in it, if present the required valu is returned.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Key's value from Mixed Dictionaries List
# Using list comprehension
  
# initializing list
test_list = [{"Gfg" : 3, "b" : 7}, 
             {"is" : 5, 'a' : 10}, 
             {"Best" : 9, 'c' : 11}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing K 
K = 'Best'
  
# list comprehension to get key's value 
# using in operator to check if key is present in dictionary
res = [sub[K] for sub in test_list if K in sub][0]
  
# printing result 
print("The extracted value : " + str(res))

chevron_right


Output

The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}]
The extracted value : 9

Method #2 : Using update() + loop

This is yet another way in which this task can be performed. In this, we update each dictionary into each other. Forming one large dictionary, and then value is extracted from this dictionary.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Key's value from Mixed Dictionaries List
# Using update() + loop
  
# initializing list
test_list = [{"Gfg" : 3, "b" : 7}, 
             {"is" : 5, 'a' : 10}, 
             {"Best" : 9, 'c' : 11}]
  
# printing original list
print("The original list : " + str(test_list))
  
# initializing K 
K = 'Best'
  
res = dict()
for sub in test_list:
      
    # merging all Dictionaries into 1
    res.update(sub)
  
# printing result 
print("The extracted value : " + str(res[K]))

chevron_right


Output

The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}]
The extracted value : 9



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.