Need some explanation | SoloLearn: Learn to code for FREE!

0

Need some explanation

print(all([]), any([])) Output: True False Why the all return "True" and any "False"

24 Answers

New Answer

+4

You can think of 'all'  being implemented as: def all(list, condition): for a in list: if not condition(a): return false return true Whereas 'any' is: def any(list, condition): for a in list: if condition(a): return true return false That is to say, all is innocent until proven guilty, and any is guilty until proven innocent.

+2

Per Bratthammar I'm not a beginner, but your explanation is not as clear as possible, as it implies mathematical skills (to understand your equations) ^^ I guess this would lost a lot of people ;P

+2

No, I did’t copy any text. It is taken straight out of my head. I acctually read a five weeks course in First-Order-Language the University of Gothenburg at the spring of 1993. Some refreshing with paper and pen, all it all come back! 🙂 I’m glad to help and like inspire people to explore new things.

+1

by convention, as empty lists are not all true nor all false ^^

+1

Hi! I agree the returns are by their defintions. So the case with a empty container (iterable) is a special case. But an empy list is always ’False’. You can try it with print(bool([])). All other lists are ’True’. /Regards Per B 🙂

+1

the python documentation says, that the "all" function returns a Boolean value, depending on which arguments it took. if the argument contains an empty object (your case), then "true" is returned. the same thing with the "any" function, only on the contrary, "false" is returned here.

+1

Per Bratthammar well this is very difficult for a beginner I think 😆

+1

so, would you explain how to read/understand such things: ∀ ∃ ∧ ∨ and how to interpret them inside equations?

+1

Okey, to say it in english: all([]) becomes True because if there are no elements in the then there are no elements that have the possibillity to do it False, so it it must become True . But in the case of any([]) there have to exust at least on elenent that is true to get it True, and because the list has no element, it must be False. How about this explanation? /Regards Per B 🙂

+1

Per Bratthammar better understandable, but it's only about convention: you could still consider that empty list are False for all and/or True for any ^^

+1

It is symbols from predicate logic. The first two are quantifiers: ∀ for all ∃ for any ∧ conjunction or ’and’ ∨ disjunction or ’or’

+1

Per Bratthammar I guess you've a typo: it should be 'from' instead of 'prom', isn't it?

+1

https://code.sololearn.com/cY1AUdqpPhIV/?ref=app

+1

Very good question and I'm glad to answer these types of questions To understand why all([]) and any([]) return True and False respectively you at least need to know how to implement these functions yourself and if you don't, don't worry!! Quick tips: all() -> Looking for False to return else return True any() -> Looking for True to return else return False Do you know that all() while in iteration once it found False value it return aka execution stopped and False is being returned. It only return True while it finished iteration and no False being met. And hence all([]) since it contains an empty list there will be no iteration aka no searches for False value and it then return True any() on the other hand, is looking for at least True value in the iteration to execute and return True. It return False after the end of iteration if no True met. Thus, any([]) does not have an item to iterate over nor to search for at least a True value hence value False which is available after the iteration is return

0

visph can you give a brief a explanation with example.

0

Hi! could you give an example of the code and explain what doesn't work there, because initially your question is not clear

0

Ярослав Вернигора(Yaroslav Vernigora) I was just saying that, how a blank square can give "True" while taken with all and "False" while taken with any.

0

The real interesting question here is why all([]) becomes True and any([]) becomes False. The question leads back to the area of first-order-logic: In our case: all([x1, x2, ..., xn]) and any([x1, x2, ..., xn]) => • P(x) - x are in the list; • Q(x) - x return True (think: bool(x)). You can write this as: • all([x,...]) <=> ∀x P(x): Q(x) <=> ∀x(P(x) → Q(x)) <=> for all x (not P(x) or Q(x)) • any([x,...]) <=> ∃xP(x): Q(x) <=> ∃x (P(x) ∧ Q(x)) <=> there exist an x such that (P(x) and Q(x)). => So when list is empty: ∀x P(x) is ‘x is in the list’, but when the list is empty x is no in the list, so P(x) become False, so ∀x Q(x) also get false (no element in list). So for all([]): For all x: not P(x) ∨ Q(x) <=> not False or False <=> True or False <=> True. And for any([]): There exist a x such that x is in the list and x is true: ∃x(P(x) ∧ Q(x)) <=> False and False <=> False, because list is empty so no such x exist. So all([]) becomes True, and any([]) becomes False. /Regards Per B 🙂

0

Well Yaroslav-Vernigora, everything in life isn’t for beginners, and shall not be. Challanges is like a spice for life, and that’s what getting us to reach the next level. Now I don’t think the asker is a beginner; he have reach level 14! And I acctually think there is an curiosity to understand the the mechanisms behind the fact that the Python bult-in functions any() and all() react so different when the argument is an empty container/iterable. /Regards Per B 🙂

0

well, for example, I began to study python recently. and for such functions, I have not even heard of it. thanks to this question, I got on the internet and studied this topic a little. but what you have written is simply cosmic, as yet unattainable knowledge. thank you so much for taking the time to explain this. or did you just copy the text?