spacing
spacing

Reputation: 780

How to apply AND to all elements of a list?

Let's say Im storing a boolean value in list with a variable called save_status

I will be storing it like this

[ True, False, True, ...]

I want to be able to apply an and to all the elements of the list like so: True and False and True, so that I know when all the elements of the list were True

Upvotes: 2

Views: 65

Answers (3)

James Sapam
James Sapam

Reputation: 16950

Trying another way:

>>> all_true = [ True, True, True]
>>> mix = [ True, False, True]
>>> sum(all_true) == len(all_true)
True
>>> sum(mix) == len(mix)
False
>>>

Upvotes: 1

Thomas Schreiter
Thomas Schreiter

Reputation: 810

Use all for "and" and any for "or":

>>> my_list = [ True, False, True ]
>>> all(my_list)
... False
>>> any(my_list)
... True

Upvotes: 4

Nick Retallack
Nick Retallack

Reputation: 19581

all([ True, False, True, ...])

Upvotes: 3

Related Questions