Getting key with maximum value in dictionary?

ricafeal picture ricafeal · Nov 6, 2008 · Viewed 984k times · Source

I have a dictionary: keys are strings, values are integers.

Example:

stats = {'a':1000, 'b':3000, 'c': 100}

I'd like to get 'b' as an answer, since it's the key with a higher value.

I did the following, using an intermediate list with reversed key-value tuples:

inverse = [(value, key) for key, value in stats.items()]
print max(inverse)[1]

Is that one the better (or even more elegant) approach?

Answer

A. Coady picture A. Coady · Nov 11, 2008
max(stats, key=stats.get)