5

The max/min built-ins seems to be greedy, i.e. they will return the first occurrence for the case when it is non-unique.

>>> x = [('spam', 1), ('egg', 0), ('potato', 1)]
>>> max(x, key=lambda v: v[1])
('spam', 1)
>>> max(reversed(x), key=lambda v: v[1])
('potato', 1)

Is this guaranteed by the language and can be relied on cross-platform and over versions, or is it implementation detail?

Charles
  • 50,943
  • 13
  • 104
  • 142
wim
  • 338,267
  • 99
  • 616
  • 750
  • 8
    It seems to be an implementation detail, as not specified in the docs: http://stackoverflow.com/questions/6783000/which-maximum-does-python-pick-in-the-case-of-a-tie/6783101#6783101 – root Feb 11 '13 at 06:38

0 Answers0