Python | Keys with Maximum value

Many times, we may have problem in which we require to find not just the value, but the corresponding keys to the maximum value in the entire dictionary. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using max() + list comprehension + values()
The combination of above functions can be used to perform this particular task. In this, maximum value is extracted using the max function, while values of dictionary is extracted using values(). The list comprehension is used to iterate through the dictionary for matching keys with max value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Keys with Maximum value
# Using max() + list comprehension + values()
  
# initializing dictionary
test_dict = {'Gfg' : 2, 'for' : 1, 'CS' : 2}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Using max() + list comprehension + values()
# Keys with Maximum value
temp = max(test_dict.values())
res = [key for key in test_dict if test_dict[key] == temp]
  
# printing result 
print("Keys with maximum values are : " + str(res))

chevron_right


Output :



The original dictionary is : {'CS': 2, 'Gfg': 2, 'for': 1}
Keys with maximum values are : ['CS', 'Gfg']

 

Method #2 : Using all() + list comprehension
This task can also be performed using list comprehension and all function. In this, we take all the elements values, using all function that are smaller than values with keys and return the keys with largest values using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Keys with Maximum value
# Using all() + list comprehension
  
# initializing dictionary
test_dict = {'Gfg' : 2, 'for' : 1, 'CS' : 2}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Using all() + list comprehension
# Keys with Maximum value
res = [key for key in test_dict if all(test_dict[temp] <= test_dict[key] for temp in test_dict)]
  
# printing result 
print("Keys with maximum values are : " + str(res))

chevron_right


Output :

The original dictionary is : {'CS': 2, 'Gfg': 2, 'for': 1}
Keys with maximum values are : ['CS', 'Gfg']



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.