Get the Key(s) corresponding to max(value) in python dict
Use max()
and list comprehension:
>>> dic = {'a' : 10, 'x' : 44, 'f': 34, 'h':89, 'j': 90, 'd': 28,"k":90}
>>> maxx = max(dic.values()) #finds the max value
>>> keys = [x for x,y in dic.items() if y ==maxx] #list of all
#keys whose value is equal to maxx
>>> keys
['k', 'j']
Create a function:
>>> def solve(dic):
maxx = max(dic.values())
keys = [x for x,y in dic.items() if y ==maxx]
return keys[0] if len(keys)==1 else keys
...
>>> solve({'a' : 10, 'x' : 44, 'f': 34, 'h':89, 'j': 90, 'd': 28})
'j'
>>> solve({'a' : 10, 'x' : 44, 'f': 34, 'h':89, 'j': 90, 'd': 28, 'g' : 90})
['g', 'j']
You can do:
maxval = max(dict.iteritems(), key=operator.itemgetter(1))[1]
keys = [k for k,v in dict.items() if v==maxval]