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