This is best done with the understanding:
>>> sum(1 for i in [1,1,1,2,2,3,0] if i) 6
or
sum(bool(i) for i in [1,1,1,2,2,3,0])
Or consider the way back, as there is no ambiguity regarding False - this is something other than 0
>>> li=[1, 1, 1, 2, 2, 3, 0] >>> len(li) - li.count(False) 6
Even better:
sum(map(bool,li))
the wolf
source share