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

GHZ geraint.williams at gmail.com
Mon Oct 29 18:57:58 EDT 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