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

I believe all([])==True is generally harder to grasp, so here are a collection of examples where I think that behaviour is obviously correct:

  • A movie is suitable for the hard of hearing if all the dialog in the film is captioned. A movie without dialog is still suitable for the hard of hearing.
  • A windowless room is dark when all the lights inside are turned off. When there are no lights inside, it is dark.
  • You can pass through airport security when all your liquids are contained in 100ml bottles. If you have no liquids you can still pass through security.
  • You can fit a soft bag through a narrow slot if all the items in the bag are narrower than the slot. If the bag is empty, it still fits through the slot.
  • A task is ready to start when all its prerequisites have been met. If a task has no prerequisites, it's ready to start.

One property of any is its recursive definition

any([x,y,z,...]) == (x or any([y,z,...]))

That means

x == any([x]) == (x or any([]))

The equality is correct for any x if and only if any([]) is defined to be False. Similar for all.


I think of them as being implemented this way

def all(seq):
    for item in seq:
        if not item:
            return False
    return True

def any(seq):
    for item in seq:
        if item:
            return True
    return False

not sure they are implemented that way though


How about some analogies...

You have a sock drawer, but it is currently empty. Does it contain any black sock? No - you don't have any socks at all so you certainly don't have a black one. Clearly any([]) must return false - if it returned true this would be counter-intuitive.

The case for all([]) is slightly more difficult. See the Wikipedia article on vacuous truth. Another analogy: If there are no people in a room then everyone in that room can speak French.

Mathematically all([]) can be written:

where the set A is empty.

There is considerable debate about whether vacuous statements should be considered true or not, but from a logical viewpoint it makes the most sense:

The main argument that all vacuously true statements are true is as follows: As explained in the article on logical conditionals, the axioms of propositional logic entail that if P is false, then P => Q is true. That is, if we accept those axioms, we must accept that vacuously true statements are indeed true.

Also from the article:

There seems to be no direct reason to pick true; it’s just that things blow up in our face if we don’t.

Defining a "vacuously true" statement to return false in Python would violate the principle of least astonishment.

Tags:

Python

Logic