5

I searched for understanding about the all function in Python, and I found this, according to here:

all will return True only when all the elements are Truthy.

But when I work with this function it's acting differently:

'?' == True   # False
'!' == True   # False
all(['?','!']) # True

Why is it that when all elements in input are False it returns True? Did I misunderstand its functionality or is there an explanation?

3 Answers 3

9

only when all the elements are Truthy.

Truthy != True.

all essentially checks whether bool(something) is True (for all somethings in the iterable).

>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True
1
  • Only bool("") is False (in string) ? every non empty string is True? Feb 28, 2016 at 17:32
1

'?' and '!' are both truthy since they are non-empty Strings.

There's a difference between True and "truthy". Truthy means that when coerced, it can evaluate to True. That's different from it being == to True though.

1
  • Downvoter, please comment. I updated the second half of my answer since I misinterpreted him. Feb 28, 2016 at 17:31
-2

all() function is used when we want to check that if all the items in a list are iterable or not. For ex : x=[1,2,3,4,5] all(x) It will return True.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.