builtin functions for and and or?

Brian Beck exogen at gmail.com
Sun Feb 13 16:40:23 EST 2005


Roose wrote:
> I need this a lot: a one line way to do a n-ary and or 'or'.

Looks like there are itertools recipes for those, similar to what 
Michael just posted. Taken from here: 
http://docs.python.org/lib/itertools-recipes.html

def all(seq, pred=bool):
     "Returns True if pred(x) is True for every element in the iterable"
     for elem in ifilterfalse(pred, seq):
         return False
     return True

def any(seq, pred=bool):
     "Returns True if pred(x) is True for at least one element in the 
iterable"
     for elem in ifilter(pred, seq):
         return True
     return False


--
Brian Beck
Adventurer of the First Order



More information about the Python-list mailing list