Getting Number of Elements in an Iterator in Python

Getting number of elements in an iterator in Python

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.

Python - Count Elements in Iterator Without Consuming

I have not been able to come up with an exact solution (because iterators may be immutable types), but here are my best attempts. I believe the second should be faster, according to the documentation (final paragraph of itertools.tee).

Option 1

def it_count(it):
tmp_it, new_it = itertools.tee(it)
return sum(1 for _ in tmp_it), new_it

Option 2

def it_count2(it):
lst = list(it)
return len(lst), lst

It functions well, but has the slight annoyance of returning the pair rather than simply the count.

ita = iter([1, 2, 3])
count, ita = it_count(ita)
print(count)

Output: 3

count, ita = it_count2(ita)
print(count)

Output: 3

count, ita = it_count(ita)
print(count)

Output: 3

print(list(ita))

Output: [1, 2, 3]

Is there any built-in way to get the length of an iterable in python?

Short of iterating through the iterable and counting the number of iterations, no. That's what makes it an iterable and not a list. This isn't really even a python-specific problem. Look at the classic linked-list data structure. Finding the length is an O(n) operation that involves iterating the whole list to find the number of elements.

As mcrute mentioned above, you can probably reduce your function to:

def count_iterable(i):
return sum(1 for e in i)

Of course, if you're defining your own iterable object you can always implement __len__ yourself and keep an element count somewhere.

What's the shortest way to count the number of items in a generator/iterator?

Calls to itertools.imap() in Python 2 or map() in Python 3 can be replaced by equivalent generator expressions:

sum(1 for dummy in it)

This also uses a lazy generator, so it avoids materializing a full list of all iterator elements in memory.

How to get length of a dictionary iterator?

An iterator can be used only once. Convert it to a list, then measure the length of the list and/or use that list for further processing:

neighbours = list(nxobject.neighbors('Something'))
print(len(neighbours))


Related Topics



Leave a reply



Submit