Given a dictionary d
where the key values pair consist of a string as key and an integer as value, I want to print the key string where the value is the maximum.
Of course I can loop over d.items()
, store the maximum and its key and output the latter after the for
loop. But is there a more "pythonic" way just using just a max
function construct like
print max(...)
print max(d.keys(), key=lambda x: d[x])
or even shorter (from comment):
print max(d, key=d.get)
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