I'm using python 2 and I want to compare between item in a list but I got stucked. Here's the problem:
x = [True, False, True, True, False]
how do i get the result to process the boolean (True & False & True & True & False)?
thanks before
2 Answers 2
Try this:
>>> x = [True, False, True, True, False]
>>> all(x)
False
I take it that by this:
(True & False & True & True & False)
you are looking for the intersection of all Boolean values. All values in the list must evaluate to True for all(x) to return True.
Comments
If I understand your question correctly, you want the all function.
x = [True, False, True, True, False]
all(x)
The all function goes from left to right within an iterable, and evaluates each term in a boolean context. If it reaches any False (or False-ish, like None) value, it returns False immediately, just as if you'd written a and b and c and d out by hand. If it encounters only truthy values, it returns True.
Related: there's a similar function, called any, that does the same thing using or instead of and. So in your case, all(x) returns False, but any(x) would return True, since there's at least one truthy value.
3 Comments
Series objects even have their own all methods already.