Per the documentation of all
:
all(iterable)
Return True
if all elements of the iterable are true (or if the iterable is empty).
And the documentation for any
:
any(iterable)
Return True
if any element of the iterable is true. If the iterable is empty, return False
.
An empty iterable []
is falsey, but it doesn't matter as the return value is just by implementation.
If you're wondering why this happens, it's just a consequence of the implementation. If you look at the equivalent code for all
from the documentation:
def all(iterable):
for element in iterable:
if not element:
return False
return True
Because of this specific implementation, if the iterable is empty, the for
loop is skipped completely as there are no elements. Thus, it returns True
. For any
, the documentation provides the equivalent code:
def any(iterable):
for element in iterable:
if element:
return True
return False
The reason it returns False
for an empty iterable is the same reason all
return True
. Since there are no elements in the list, the for
loop is skipped and it returns False
.
This implementation does have a reasoning, since empty set logic makes all
return true, see this Math.SE post and this SO answer. all
can be thought of as "there are as many true elements as elements". Since an empty set has no true elements and no elements, it returns true because 0 equals 0. any
can be thought of as "there's at least one...", and since the set is empty, there's not at least one because there is not even one element. Thus all
returns true for an empty set, and any
returns false for an empty set.