1

So I have read all the documentation on programiz and I still can't figure out why these run as True. Code:

yey = [False]
yea = [0]

print(all((yea, yey)))
print(any((yea, yey)))

output:

True
True
ewe
  • 159
  • 1
  • 7
  • 6
    Does this answer your question? [How do Python's any and all functions work?](https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work) – Tom Ron Jun 28 '20 at 06:05
  • btw progamiz is a terrible way to learn any language – qwr Jun 28 '20 at 06:09
  • not really @TomRon Thank you for the suggestion though – ewe Jun 28 '20 at 06:13

3 Answers3

2

The values of the single element inside your lists are 0 and False:

yey = [False]
yea = [0]

But when you use yea and yey, in all and any, the check is "is the list empty or not?

print(all((yea, yey)))
print(any((yea, yey)))

Will return True because the lists are not empty, but if you look at the first elements for example:

print(all((yea[0], yey[0])))
print(any((yea[0], yey[0])))

returns

False
False

Here is a function you could use to check multiple lists:

yey = [False]
yea = [0]

def any_list(lists): return any([any(i) for i in lists])
def all_list(lists): return all([all(i) for i in lists])

print(all_list((yea, yey)))   # False
print(any_list((yea, yey)))   # False
leopardxpreload
  • 767
  • 5
  • 17
2

Because bool(yea) evaluates to True (list has element)

>>> all((yea, yey))
True
>>>
>>> (yea, yey)
([0], [False])
>>>
>>> (bool(yea), bool(yey))
(True, True)
>>>

So,

>>> all((True, True))
True
>>> any((True, True))
True
>>>

And

>>> all((False, 0))
False
>>>
>>> any((False, 0))
False
>>>
kundan
  • 1,278
  • 14
  • 27
0

Most likely you want something more like

>>> all(yea)
False
>>> any(yea)
False

These would make even more sense if yea and yay were each a list with multiple elements rather than a single value.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Yea it works that way but I was just not understanding why it does not work with two arguments – ewe Jun 28 '20 at 06:14