sum(1 for x in iterable)
is the best method. Not everything requires a dedicated itertools
function. :-) I note that the top-voted answer on the post you claim not to be a dupe of also advices you to use this.
Of course, it is always valuable to look at itertools
just in case, and if you do, don't forget to check the recipes section; you'll find the quantify()
recipe which does pretty much the same thing, but with a predicate to filter the iterable:
def quantify(iterable, pred=bool):
"Count how many times the predicate is true"
return sum(imap(pred, iterable))