Getting number of elements in an iterator in Python Getting number of elements in an iterator in Python python python

Getting number of elements in an iterator in Python


This code should work:

>>> iter = (i for i in range(50))>>> sum(1 for _ in iter)50

Although it does iterate through each item and count them, it is the fastest way to do so.

It also works for when the iterator has no item:

>>> sum(1 for _ in range(0))0

Of course, it runs forever for an infinite input, so remember that iterators can be infinite:

>>> sum(1 for _ in itertools.count())[nothing happens, forever]

Also, be aware that the iterator will be exhausted by doing this, and further attempts to use it will see no elements. That's an unavoidable consequence of the Python iterator design. If you want to keep the elements, you'll have to store them in a list or something.


No. It's not possible.

Example:

import randomdef gen(n):    for i in xrange(n):        if random.randint(0, 1) == 0:            yield iiterator = gen(10)

Length of iterator is unknown until you iterate through it.


No, any method will require you to resolve every result. You can do

iter_length = len(list(iterable))

but running that on an infinite iterator will of course never return. It also will consume the iterator and it will need to be reset if you want to use the contents.

Telling us what real problem you're trying to solve might help us find you a better way to accomplish your actual goal.

Edit: Using list() will read the whole iterable into memory at once, which may be undesirable. Another way is to do

sum(1 for _ in iterable)

as another person posted. That will avoid keeping it in memory.