Minimum of a dictionary
Magnus Lie Hetland
mlh at idi.ntnu.no
Fri May 18 21:52:34 EDT 2001
In the current CVS version of Python, min() and max()
over dictionaries return resp. the minimum and
maximum key. This seems logical once the "key in dict"
syntax has been accepted. However, I find that the
need to extract the key which corresponds to the
minimum value (or resp. the maximum value) often
presents itself in real applications... For instance:
age["Fnord"] = 57
.
.
.
oldest = key_with_max_val(age)
Is there (or will there be) a built-in mechanism
for this sort of thing? Otherwise, what would be the
most pythonic way of doing it?
I mean, I can think of things like:
oldest = max([(val, key) for (key, val) in age.items()])[1]
but it doesn't look very pretty.
perhaps-I-should-just-make-myself-a-Fibonacci-heap'ly yrs,
- Magnus
--
Magnus Lie Hetland http://www.hetland.org
"Reality is that which, when you stop believing in
it, doesn't go away." -- Philip K. Dick
More information about the Python-list
mailing list