45 – Counting values that satisfy a predicate#
(This is my favourite line of Python code. Really.)
To count the values of an iterable that satisfy a given predicate (a function that returns True/False) or a given condition, use the built-in sum and a generator expression:
sum(predicate(value) for value in iterable)
This idiom works in 3 parts:
the generator expression goes over all values you want to consider;
predicate(value)evaluates the condition, producingTrueorFalse; andthe built-in
sumaccumulates all the Boolean values, effectively counting the number ofTrues.
If you actually have a predicate function, you might prefer the version sum(map(predicate, iterable)).
If you want to use an ad-hoc expression as the condition, then use the generator expression:
ages = [42, 73, 16, 10, 4, 6]
can_vote = sum(age > 18 for age in ages)
print(can_vote) # 2