Print the maximum value key in the Pythonic dictionary

Given a dictionary d , where a pair of key values โ€‹โ€‹consists of a string as a key and an integer as a value, I want to print a key string where the value is the maximum.

Of course, I can d.items() over d.items() , save the maximum and its key and print the last one after the for loop. But is there a more "pythonic" way using only the max function construct, such as

 print max(...) 
+8
python max
source share
1 answer
 print max(d.keys(), key=lambda x: d[x]) 

or even shorter (from the comment):

 print max(d, key=d.get) 
+16
source share

All Articles