Checking the boolean value of a collection

Fredrik Lundh fredrik at pythonware.com
Fri Sep 12 10:46:49 EDT 2008


Marco Bizzarri wrote:

> Can you clarify where I can find "any"? It seems to me I'm
 > unable to find it...

it's a 2.5 addition.  to use this in a "future-compatible" way in 2.3, 
you can add

      try:
          any
      except NameError:
          def any(iterable):
              for element in iterable:
                  if element:
                      return True
              return False

to the top of the file (or to some suitable support library).

2.5 also provides an "all" function, which can be emulated as:

      try:
          all
      except NameError:
          def all(iterable):
              for element in iterable:
                  if not element:
                      return False
              return True

</F>




More information about the Python-list mailing list