Take the 2-minute tour ×

What's the idiomatic way to do maximumBy (higher order function taking a comparison function for the test), on a list of lists, where the comparison we want to make is the sum of the list, in Python?

Here's a Haskell implementation and example output:

> maximumBy (compare `on` sum) [[1,2,3],[4,5,6],[1,3,5]]
> [4,5,6]

And implementations of those base library functions, just for completeness (in case you want to use reduce or something :)

maximumBy cmp xs =  foldl1 maxBy xs
       maxBy x y = case cmp x y of GT -> x; _ -> y

k `on` f = \x y -> f x `k` f y

sum      =  foldl' (+) 0
share|improve this question

3 Answers 3

up vote 38 down vote accepted

Since Python 2.5 you can use max with a key parameter:

>>> max(a, key=sum)
[4, 5, 6]
share|improve this answer
Haha, awesome solution! –  Xavier Ho May 4 '10 at 5:36
+1. Short, clear and precise. Very pythonic. –  Noufal Ibrahim May 4 '10 at 5:42
max(a) seems to do the same thing. –  titaniumdecoy May 4 '10 at 5:43
@titaniumdecoy: The default behaviour is a lexicographical ordering which by chance happens to give the same result for this input data, but in general can give a different answer. –  Mark Byers May 4 '10 at 5:46
how can you now find the index of this max sublist. in the case above it would be 2 –  MySchizoBuddy Jul 14 '13 at 13:21

It isn't terribly efficient, but:

reduce(lambda x,y: x if sum(x)>sum(y) else y, [[1,2,3],[4,5,6],[1,3,5]])
share|improve this answer
Comparing this with @Mark's correct answer is a delightful way to show why Python programmers would be by far best off forgetting about reduce (good thing it's not a built-in any more in Py3!-). –  Alex Martelli May 4 '10 at 5:48
@Alex: If someone had posted an answer using a for-loop, which would have also been much clumsier than Mark's answer, would you have argued that python programmers are better off forgetting about for-loops? –  sepp2k May 4 '10 at 9:58
@sepp2k: for loops are a delightful way of showing why Python programmers should use list comprehensions. In all seriousness, there isn't always a replacement for a for loop, but that's usually not the case for reduce. As much as I personally like the map/filter/reduce trinity, Alex is right in that there usually is a better way. –  outis May 4 '10 at 12:28
@outis, I would say "there usually is a better way in Python" -- I wouldn't say it without the qualification since it may not be the case in, e.g., Haskell or Lisp. But, we do agree on the gist;-). –  Alex Martelli May 4 '10 at 13:46
You can only eliminate the reduce in this case because max is itself essentially reduce hard-coded to use the binary max function. This does nothing whatsoever to demonstrate that reduce isn't useful in the general case. It just demonstrates that if, for your particular case, there is a specialised function, you should use it. –  sigfpe May 4 '10 at 14:41

If max didn't have the key parameter you could code the DSU pattern explicitly:


izip and imap are from the itertools module in python 2 and do what zip and map do, but lazily using Python generators, to avoid consing an intermediate list. In Python 3, the map and zip builtins are lazy.

share|improve this answer

Your Answer


