小能豆

Length of generator output

py

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
  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.

P.P.S.: re-checked… Disregard the P.S., seems I made a mistake while trying that, it works fine. Sorry for the trouble.


阅读 75

收藏
2023-11-28

共1个答案

小能豆

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:

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.

2023-11-28