Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

logic

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.)

like image 738
detly Avatar asked Jul 18 '10 09:07

detly


People also ask

Why all of empty list is true?

It's true because for every element in the list, all 0 of them, they all are equal to 2. That is to say, all is innocent until proven guilty, and any is guilty until proven innocent.

Why do I get an empty list in Python?

You can create an empty list using an empty pair of square brackets [] or the type constructor list() , a built-in function that creates an empty list when no arguments are passed. Square brackets [] are commonly used in Python to create empty lists because it is faster and more concise.

What does an empty list evaluate to?

In Python, empty list object evaluates to false. Hence following conditional statement can be used to check if list is empty.

How do you define an empty list?

In python, an empty list is defined as the list with no elements or items on the list. To define an empty list in Python, there are two ways to do this, and they are done either by using square bracket [] or by using a list constructor such as list().


2 Answers

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.

like image 162
Mark Byers Avatar answered Sep 21 '22 18:09

Mark Byers


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.

like image 25
kennytm Avatar answered Sep 23 '22 18:09

kennytm