sorting a dictionary
Andrew Bennetts
andrew-pythonlist at puzzling.org
Tue Feb 4 02:55:05 EST 2003
On Mon, Feb 03, 2003 at 10:56:20PM -0800, Yuval wrote:
> I'm looking for an efficient way to get the key of the highest value
> in a dictionary.
> For example, for dict = {"a":10, "b":5, "c":15}, I want to write a
> function get_highest(dict), that will return c as the result (since c
> is the key of the highest value in the dict.
def get_highest(d):
return max([(x[1], x[0]) for x in d.items()])[1]
Tested:
>>> def get_highest(d):
... return max([(x[1], x[0]) for x in d.items()])[1]
...
>>> get_highest({"a":10, "b":5, "c":15})
'c'
-Andrew.
More information about the Python-list
mailing list