On Oct 29, 6:57 pm, GHZ <[EMAIL PROTECTED]> wrote:
> Is this the best way to test every item in a list?
>
> def alltrue(f,l):
>     return reduce(bool.__and__,map(f,l))
>
> def onetrue(f,l):
>     return reduce(bool.__or__,map(f,l))

Probably not, because it doesn't take advantage of short circuiting.
You could bail out of alltrue as soon as the first item you see is
false, but your version applies f to every item in the list.  I would
suggest the most straightforear way is the best:

def alltrue(f,l):
    for item in l:
        if not f(item):
            return False
    return True


On Python 2.5, you could do this:

all(f(x) for x in l)


Carl Banks

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to