What's the shortest way to count the number of items in a generator/iterator?
If I want the number of items in an iterable without caring about the elements themselves, what would be the pythonic way to get that? Right now, I would define def ilen(it): return sum(itertools.imap(lambda _: 1, it)) # or just map in Python 3 but I understand lambda is close to being considered harmful, and lambda _: 1 certainly isn't pretty. (The use case of this is counting the number of lines in a text file matching a regex, i.e. grep -c .) The usual way is sum(1 for i in it) Method that's meaningfully faster than sum(1 for i in it) when the iterable may be long (and not meaningfully