This gives the max value in a dict, but how do I get the dict key for the max value?
max([d[i] for i in d])
Use the key=
keyword argument to max()
:
max(d, key=lambda k: d[k])
Instead of the lambda you can use operators.itemgetter
as well:
import operators
max(d, key=operators.itemgetter(d))
or pass in d.get
:
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