Python – Extract Maximum Keys’ value dictionaries

Given a dictionary, extract all the dictionary which contains a any key which has maximum values among other keys in dictionary list.

Input : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 14}]
Output : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 3, “is” : 6, “Best” : 14}]
Explanation : “Gfg” has 9 as best, “is” has 7 and “Best” has 14, those dictionaries are extracted.

Input : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 5, “is” : 4, “Best” : 10}, {“Gfg” : 3, “is” : 6, “Best” : 16}]
Output : [{“Gfg” : 3, “is” : 7, “Best” : 8}, {“Gfg” : 9, “is” : 2, “Best” : 9}, {“Gfg” : 3, “is” : 6, “Best” : 16}]
Explanation : “Gfg” has 9 as best, “is” has 7 and “Best” has 16, those dictionaries are extracted.

Method : Using max() + filter() + lambda

The combination of above functionalities can be used to solve this problem. In this, first maximum is extracted for particular key and then all dictionaries matching maximum key are extracted. This is carries out for all the keys.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Maximum Keys' value dictionaries
# Using max() + filter() + lambda
  
# initializing list
test_list = [{"Gfg" : 3, "is" : 7, "Best" : 8}, 
             {"Gfg" : 9, "is" : 2, "Best" : 9}, 
             {"Gfg" : 5, "is" : 4, "Best" : 10},
             {"Gfg" : 3, "is" : 6, "Best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
res = []
  
# getting all keys 
all_keys = list(test_list[0].keys())
for sub in all_keys:
      
    # extracting maximum of each keys
    temp = max(test_list, key = lambda ele: ele[sub]) 
    res_key = list(filter(lambda ele: ele[sub] == temp[sub], test_list))
    res.append(res_key)
  
# printing result 
print("The extracted maximum key values dictionaries : " + str(res))

chevron_right


Output

The original list : [{'Gfg': 3, 'is': 7, 'Best': 8}, {'Gfg': 9, 'is': 2, 'Best': 9}, {'Gfg': 5, 'is': 4, 'Best': 10}, {'Gfg': 3, 'is': 6, 'Best': 8}]
The extracted maximum key values dictionaries : [[{'Gfg': 9, 'is': 2, 'Best': 9}], [{'Gfg': 3, 'is': 7, 'Best': 8}], [{'Gfg': 5, 'is': 4, 'Best': 10}]]



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.