short-circuiting any/all ?

Raymond Hettinger python at rcn.com
Mon Mar 22 19:27:57 CET 2010


On Mar 22, 7:45 am, kj <no.em... at please.post> wrote:
> I have a list of items L, and a test function is_invalid that checks
> the validity of each item.  To check that there are no invalid
> items in L, I could check the value of any(map(is_invalid, L)).
> But this approach is suboptimal in the sense that, no matter what
> L is, is_invalid will be executed for all elements of L, even though
> the value returned by any() is fully determined by the first True
> in its argument.  In other words, all calls to is_invalid after
> the first one to return True are superfluous.  Is there a
> short-circuiting counterpart to any(map(is_invalid, L)) that avoids
> these superfluous calls?
>
> OK, there's this one, of course:
>
> def _any_invalid(L):
>     for i in L:
>         if is_invalid(i):
>             return True
>     return False  
>
> But is there anything built-in?  (I imagine that a lazy version of
> map *may* do the trick, *if* any() will let it be lazy.)

Yes, that will work:

      from itertools import imap  # lazy version of map
      any(imap(is_invalid, L)     # short-circuits on first True

Yet another approach (slightly faster):

   from itertools import ifilter
   any(ifilter(is_invalid, L))


Raymond






More information about the Python-list mailing list