for_some(), for_all()?

aurora aurora00 at gmail.com
Wed Sep 22 12:57:21 EDT 2004


I am trying to test a list to see if some or all elements pass a test. For  
example I can use reduce() to test if any element is 0.

>>> lst = [1,2,0,3]
>>> test = lambda x: x == 0
>>> reduce(operator.__or__,[test(x) for x in lst])
True

However I bet reduce() does not exploit the short circuit logic. Also it  
is a little clumsy to create another list to pass into reduce. Is there  
some equivalent of

   for_some(test, lst)
or
   for_all(test, lst)?





More information about the Python-list mailing list