all([]) True, but any([]) False. What's going on here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

all([]) True, but any([]) False. What's going on here?

I'd expect that all items in the first container would have to be True to get a True. Alright, so maybe if you arg an empty list, the list itself as an object is True... or so my guessing. But why is then the second condition False? oO

10th Jul 2018, 9:17 PM
HonFu
HonFu - avatar
8 Answers
+ 6
all(list1) returns True whenever every element in list1 is True. Here we are saying, "Whenever an element is in list1, it must be True." Or roughly speaking, "Get me an element from list1, and I'll prove that it's true." Thus, when there are no elements in list1, the original assertion still remains true. Just like a person won't be lying when they say "If elephants can fly, then so can I." We call these vacuously true statements: https://en.m.wikipedia.org/wiki/Vacuous_truth On the other hand, any(list1) returns True when at least one element in list1 is True. The obligation is on us now: unless we can actually exhibit an element in list1 that is True, it will return False. Since there is no such element in an empty list, any([]) returns False. Hope that makes sense :)
12th Jul 2018, 10:36 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 9
As help(any) and help(all) explain, respectively -- if the iterator argument is empty, the function returns False and True, accordingly :)
10th Jul 2018, 9:21 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
So this is just an implementation thing? No deeper meaning? (Help-function - a good reminder, thanks! '^^)
10th Jul 2018, 9:24 PM
HonFu
HonFu - avatar
+ 3
Thanks, Kishalaya Saya, I never heard of that concept before. Another example on the Wikipedia is: 'I ate every vegetable on my plate!' - when there were no vegetables on that plate. In a way, 'while' kind of works like that, too, right? 'while len(container) >10: Do this and that' also works (by not doing anything) when there's nothing in the container in the first place (not sure if I am digressing here). But couldn't we apply the same logic to any? 'I ate some of the vegetables on my plate (but there weren't any).' Why isn't this (nonsensically) true in the same way, since there's no data to prove the opposite?
12th Jul 2018, 11:07 AM
HonFu
HonFu - avatar
+ 3
Kishalaya, thanks for your elaboration! I think you explain it very well, it's not the fault of your English either... it's just that this sort of logic is already far from what non-mathematically-inclined people normally do. If I entered an empty living room and said: All the party guests here are wearing black tuxedos, someone probably would call a doctor. ;-) Or at least say something like: 'What the hell are you talking about? Noone's here!!' On the other hand there's that joke even about negative numbers: 'If there are minus 2 people in the room, two have to enter so that it's empty...' So thank you for leading me one step further from (or into?) mathematical confusion! :)
12th Jul 2018, 8:41 PM
HonFu
HonFu - avatar
+ 2
[1/2] It's how the logic is set up I guess: to prove a "Some..." statement, we produce an example; and to prove an "All..." statement, we show no counterexample exists. But let me still try to give more examples to illustrate. Suppose I claim "All crows are black". Then I'm given a random crow, and I just prove that it's black. It will be false when we have a counterexample: one crow that is not black. Thus the negation of the statement will be "Some crows are not black." Or more mathematically, " There exists a crow that is not black." Now when I say "Some men have black hair", (formally, "There exists a man who has black hair"), it's my job to find a black-haired man! Here the negation would be "All men have non-black hair." [Cotinued...]
12th Jul 2018, 12:23 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
[2/2] Same happens with the empty set. When the kid says, "I ate some of the vegetables on my plate", mathematically they are saying, "There was at least one vegetable on my plate that has been eaten by me." Once again, it's the kid's job to exhibit the vegetable they ate. P.S. Sorry some of the statements sound super weird; I'm not a native speaker, and turning English into Math is hard sometimes.
12th Jul 2018, 12:28 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
You're welcome, HonFu! That joke about negative numbers is one of my favorites! :) Yes, some mathematical statements make little sense in real life. But just like the joke you mentioned, they are often generalized versions of conventions that arise from reality.
13th Jul 2018, 3:44 AM
Kishalaya Saha
Kishalaya Saha - avatar