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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With