The only thing this does for me is ask why its not named count instead of sum.
Not what actually happens but conceptually:
ages = [17, 13, 18, 30, 12]
sum(age > 17 for age in ages)
=> sum([False, False, True, True, False])
=> sum([0, 0, 1, 1, 0])
=> 2 # via conventional summing
Since True and False are 1 and 0 for arithmetic in Python, this is just a regular sum which also happens to produce a count.If your input was a numpy array to begin with you could skip the array comprehension, and shorten it to numpy.count_nonzero(ages > 17), since numpy automatically broadcasts the comparison operation to each element of the array.
from collections import Counter
total = Counter(range(10)).total()
assert total == 10