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
.
To count the number of TRUE entries, which here is 5, the formula =COUNTIF(A1:A6,TRUE) applied to the column should work, but it always returns the result 1. On the other hand, the formula =COUNTIF(A1:A6,FALSE) works correctly on a similar column got by pulling down FALSE.
If you use Python Count() function to find the number of occurrences of '1' in that string, you get desired output within milliseconds. The python count() function is an inbuilt function of python which is used to count the number of occurrences of a character or substring in the string.
True
is equal to 1
.
>>> sum([True, True, False, False, False, True]) 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With