min() & max() vs sorted()

MRAB google at mrabarnett.plus.com
Thu Sep 14 20:35:16 EDT 2006


Hi,

Some time after reading about Python 2.5 and how the built-in functions
'min' and 'max' will be getting a new 'key' argument, I wondered how
they would treat those cases where the keys were the same, for example:

L = ["four", "five"]
print min(L, key = len), max(L, key = len)

The result is:

('four', 'four')

I would've thought that min(...) should return the same as
sorted(...)[0] (which it does) and that max(...) should return the same
as sorted(...)[-1] (which it doesn't).

I think that's just down to a subtlety in the way that 'max' is written.




More information about the Python-list mailing list