Length of generator output

Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn’t find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like: def iterlen(x): n = 0 try: while True: next(x) n += 1 except StopIteration: pass … Read more