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:
len(x)
def iterlen(x): n = 0 try: while True: next(x) n += 1 except StopIteration: pass return n
But I can’t get rid of a feeling that I’m reimplementing a bicycle.
(While I was typing the function, a thought struck my mind: maybe there really is no such function, because it “destroys” its argument. Not an issue for my case, though).
P.S.: concerning the first answers - yes, something like len(list(x)) would work too, but that drastically increases the usage of memory.
len(list(x))
P.P.S.: re-checked… Disregard the P.S., seems I made a mistake while trying that, it works fine. Sorry for the trouble.
It’s true that for lazy iterables like generator comprehensions or functions, there isn’t a built-in len function. However, you don’t need to manually consume the iterator to find its length. You can use the sum function with a generator expression, which is both concise and idiomatic:
len
sum
def iterlen(x): return sum(1 for _ in x) # Example usage: lazy_iterable = (x for x in range(10)) length = iterlen(lazy_iterable) print(length)
In this example, sum(1 for _ in x) effectively counts the number of elements in the lazy iterable x. It works by consuming the elements one by one without storing them in memory. This approach is memory-efficient and doesn’t require explicitly handling StopIteration.
sum(1 for _ in x)
x
StopIteration