Counting the number of True Booleans in a Python List

acs picture acs · Oct 7, 2012 · Viewed 161.9k times · Source

I have a list of Booleans:

[True, True, False, False, False, True]

and I am looking for a way to count the number of True in the list (so in the example above, I want the return to be 3.) I have found examples of looking for the number of occurrences of specific elements, but is there a more efficient way to do it since I'm working with Booleans? I'm thinking of something analogous to all or any.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Oct 7, 2012

True is equal to 1.

>>> sum([True, True, False, False, False, True])
3