Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key of max value in a dict

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])
like image 317
Baz Avatar asked Dec 08 '22 19:12

Baz


1 Answers

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)
like image 192
Martijn Pieters Avatar answered Dec 11 '22 09:12

Martijn Pieters