"and" and "or" on every item in a list

GHZ geraint.williams at gmail.com
Mon Oct 29 23:57:58 CET 2007


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))

>>> alltrue(lambda x:x>1,[1,2,3])
False
>>>
>>> alltrue(lambda x:x>=1,[1,2,3])
True
>>>

Thanks




More information about the Python-list mailing list