Reason for "all" and "any" result on empty lists

Viewed 4325

In Python, the built-in functions all and any return True and False respectively for empty iterables. I realise that if it were the other way around, this question could still be asked. But I'd like to know why that specific behaviour was chosen. Was it arbitrary, ie. could it just as easily have been the other way, or is there an underlying reason?

(The reason I ask is simply because I never remember which is which, and if I knew the rationale behind it then I might. Also, curiosity.)

10 Answers

The official reason is unclear, but from the docs (confirming @John La Rooy's post):

all(iterable)

Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

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

any(iterable)

Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

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

See also the CPython-implementation and comments.

  • all([]) == True: zero out of zero - check
  • any([]) == False: anyone? nobody - fail
Related