Python etc / Generator length

Generator length

Sometimes you need to know the size of a generator without retrieving the actual values. Some generators support len(), but this is not the rule:

In : len(range(10000))
Out: 10000

In : gen = (x ** 2 for x in range(10000))
In : len(gen)
TypeError: object of type 'generator' has no len()

The straightforward solution is to use an intermediate list:

In : len(list(gen))
Out: 10000

Though fully functional, this solution requires enough memory to store all the yielded values. The simple idiom allows to avoid such a waste:

In : sum(1 for _ in gen)
Out: 10000