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 t
True
is equal to 1
.
>>> sum([True, True, False, False, False, True])
3
I prefer len([b for b in boollist if b is True])
(or the generator-expression equivalent), as it's quite self-explanatory. Less 'magical' than the answer proposed by Ignacio Vazquez-Abrams.
Alternatively, you can do this, which still assumes that bool is convertable to int, but makes no assumptions about the value of True:
ntrue = sum(boollist) / int(True)