How is max supposed to work, especially key.

Jussi Piitulainen jpiitula at ling.helsinki.fi
Thu Dec 4 05:45:22 EST 2014


Albert van der Horst writes:

> Useful as that function [Python's max with a key] may be, it
> shouldn't have been called max.

The meaning of the key should be added to help(max), if it still isn't
- "returns a maximal element or an element that maximizes the key".

In some communities they call it arg max, with a thin space between
the components of the name. Or maybe no space: argmax.

Would you also want sorted called something else when used with a key?
Because it doesn't produce a sorted list of the keys either:

  >>> data = ("short", "long", "average")

  >>> sorted(data, key=len)
  ['long', 'short', 'average']
  >>> max(data, key=len)
  'average'

  >>> sorted(map(len, data))
  [4, 5, 7]
  >>> max(map(len, data))
  7

While the key is not a filter, a filter is as available as the map
above:

  >>> max(filter(lambda i : i % 17 == 0, range(100, 200)))
  187



More information about the Python-list mailing list