why any( ) instead of firsttrue( ) ?

Raymond Hettinger python at rcn.com
Wed Jun 9 02:54:18 EDT 2010


On Jun 8, 2:16 pm, danieldelay <danielde... at gmail.com> wrote:
>    def firsttrue(iterable):
>      for element in iterable:
>          if element:
>              return element
>      return None
>
> This function "firsttrue( )" could probably be used anywhere "any( )" is
> used, but with the ability to retrieve the first element where
> bool(element) is True, which may be sometimes usefull.

FWIW, it's not hard to roll your own fast itertools variants of any()
and all():

   next(ifilter(None, d), False)      # first true, else False

   next(ifilterfalse(None, d), True)  # first false, else True

Raymond





More information about the Python-list mailing list