Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Python built in "all" function returns True for empty iterables?

Tags:

python

I know it has a good reason, but I want to know what reason?

>>> print all([])
True

If all() is intended to check if every item on iterable evaluates to "True", and we know empty lists are evaluated to False

>>> bool([])
False

So why the all() returns True for empty lists?

< edit >

I already read the docs, and I know the implementation

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

But the question is why not?

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

There is a logic on this? if you have a list of done-tasks

today_todo_status = [task.status for task in my_todo if task.date == today]
can_i_go_home = all(today_todo_status)

Ok, on the above hypothetical example it really makes sense, if I have no tasks, so I can go home.

But there are other cases and I dont think all() was made for todo lists.. LOL

< /edit >

like image 571
Bruno Rocha - rochacbruno Avatar asked Aug 16 '12 01:08

Bruno Rocha - rochacbruno


People also ask

Why does all return true?

all() Return True if all elements of the iterable are true. If the iterable is empty, it return True. This is how I think about the any() and all() functions: any() is like evaluating multiple "or" statements.

Why all is true in Python?

Definition and Usage The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True.

Why does Python not return 0 true?

By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object. Here are most of the built-in objects considered false: Constants defined to be false: None and False.

What does return true means in Python?

Python all() function accepts a single parameter: iterable = can be an iterable object such as a list, tuple, dictionary, set, etc. The all() function returns a Boolean value: TRUE if all of the elements in the iterable are true & also if the iterable is empty. FALSE if any element in an iterable is false.


3 Answers

This is expressed as "For all X in S, X is true". If S is empty, there are no X. However, the truth statement remains True, because for all X, X was true... there just aren't any X!

Here is a explanation using logic.

Consider two sets A and B where A+B is the union of the two sets.

If any(A+B) = True -> any(A) or any(B) = True but we cannot assert either any(A)=True or any(B)=True.

If any(A+B) = False -> any(A) = False and any(B) = False.

If all(A+B) = True -> all(A)=True and all(B)=True

if all(A+B) = False -> all(A)=False or all(B)=False but we cannot assert either all(A)=False or all(B)=False.

Now instead of B, let's add the empty set Ø to A. We want to come up logic such that adding the empty set does not change the values of all() or any(), since A+Ø=A.

any(A+Ø) = any(A) or any(Ø)

any(Ø) must be False, so that if any(A) is True, any(A+Ø) is True, and if any(A) is False, any(A+Ø) is False.

all(A+Ø) = all(A) and all(Ø)

if all(A) is True, all(A+Ø) is True. Therefore, all(Ø) is True.

like image 85
Interrobang Avatar answered Oct 18 '22 21:10

Interrobang


all() (documented to "Return True if all elements of the iterable are true (or if the iterable is empty).") is equivalent to the following:

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

Since there are no elements, it will skip the loop and return True.

like image 10
Cat Avatar answered Oct 18 '22 22:10

Cat


Suppose all([]) is False.

Then, for all non empty list A, all(A + []) should be also False as

all(A + []) = all(A) and all([])
            = all(A) and False
            = False

Since A + [] = A, we know

all(A + []) = all(A) for any non empty list A

But, all(A) could be True (e.g., A = [True])

Hence,

for all non empty list A, all(A + []) should be also False

This contradicts. As a result, the first assumption is wrong and

all([]) is True

like image 2
Mo... Avatar answered Oct 18 '22 23:10

Mo...