max() of a list of tuples

Anton Vredegoor anton at
Fri Jan 24 11:43:16 CET 2003

On Thu, 23 Jan 2003 16:54:26 -0800, Erik Max Francis <max at>

>The vagueness here is, of course, because reduce works on _any_ function
>that accumulates values over any types of elements, not just numeric
>	reduce(f, S, i)
>is really precisely equivalent to
>	reduce(f, [i] + S),

This provides me with a rationale to reuse some code that I did not
post because shorter solutions were already found. It can be used for
lists that are nested deeper than one level.


from operator import getitem

def maxat(L,x):
    def getat(indices):
        return reduce(getitem,indices,L)
    def imax(i,j):
        if getat([i]+x)<getat([j]+x): return j
        return i
    return L[reduce(imax,range(len(L)))]
def test():
    L = [(1,3,5), (8,16,2), (2,56,4)]
    print maxat(L,[2])

if __name__=='__main__':

More information about the Python-list mailing list