[Tutor] using the reduce function

don arnold darnold02 at sprynet.com
Fri Jan 23 06:36:01 EST 2004

----- Original Message -----
From: "Karl Pflästerer" <sigurd at 12move.de>
To: <tutor at python.org>
Sent: Thursday, January 22, 2004 6:57 PM
Subject: Re: [Tutor] using the reduce function

On 23 Jan 2004, Karl Pflästerer <- sigurd at 12move.de wrote:


here is another solution; it's a bit nearer to the imperative one.  It
uses reduce with a default value (it wil also be a bit faster than the
first one).

def longer2 (ln, seq):
    lns = len(seq)
    if ln > lns: return ln
    else: return lns

def longest2 (seq):
    return reduce(longer2, seq, 0)

I think there is no further explaination necessary.


[my reply:]

I may be off-base here (since I've never used reduce() before), but can't
you just do this:

>>> reduce(lambda x,y: max(x,len(y)),['123456','23','1234567','1'],0)


More information about the Tutor mailing list