Getting number of elements in an iterator in Python
You cannot (except the type of a particular iterator implements some specific methods that make it possible).
Generally, you may count iterator items only by consuming the iterator. One of probably the most efficient ways:
import itertools
from collections import deque
def count_iter_items(iterable):
"""
Consume an iterable not reading it into memory; return the number of items.
"""
counter = itertools.count()
deque(itertools.izip(iterable, counter), maxlen=0) # (consume at C speed)
return next(counter)
(For Python 3.x replace itertools.izip
with zip
).
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 random
def gen(n):
for i in xrange(n):
if random.randint(0, 1) == 0:
yield i
iterator = 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.