0

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

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

the all(x) function never cross my mind before, thanks. but now it get more complex. what if the items in the list is a pandas boolean series?
@wrez pandas Series objects even have their own all methods already.
henry, it seems that you've done your little research on pandas series ;) appreciate it. if you dont mind, answering my question here will cost me a lot, thanks before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.