What is the most 'pythonic' way to logically combine a list of booleans?
See all(iterable)
:
Return
True
if all elements of the iterable are true (or if the iterable is empty).
And any(iterable)
:
Return
True
if any element of the iterable is true. If the iterable is empty, returnFalse
.
The best way to do it is with the any()
and all()
functions.
vals = [True, False, True, True, True]
if any(vals):
print "any() reckons there's something true in the list."
if all(vals):
print "all() reckons there's no non-True values in the list."
if any(x % 4 for x in range(100)):
print "One of the numbers between 0 and 99 is divisible by 4."