Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key to maxima of dictionary in python

I have a dictionary, "scores", of integers and I want to find the key(s) of the highest value. I used this code:

key = max(scores, key=scores.get)

however, this only gives back one key. How does this deal with ties in highest value? I only get one number back. Which is it in the case of a tie? How can I get all the keys to the highest value? Thanks for your help.

like image 257
Double AA Avatar asked Dec 17 '22 11:12

Double AA


1 Answers

You could run the following, for example:

max_value = max(scores.values())
keys = [ i for (i,v) in scores.iteritems() if v == max_value ]

"keys" would now hold all the keys which correspond to the maximum value.

like image 173
Pascal Bugnion Avatar answered Dec 18 '22 23:12

Pascal Bugnion