Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Is it possible to know how many iterations are in an iterator object beforehand?

So far if I wanted to know how many iterations there are in an iterator (in my case that's how many protein sequences in a file) I did:

count = 0
for stuff in iterator:
    count += 1
print count

However, I want to seperate the iterator into half so I need to know the total amount of iterations. Is there a way to know the amount of iterations there will be without looping through the iterator?

like image 422
Niek de Klein Avatar asked Dec 04 '22 05:12

Niek de Klein


2 Answers

There is no way to know how many values an iterator will produce without consuming it until the end. Note that an iterator can also be infinite, so in that case the total count is not even defined.

If you can ensure the iterator to be finite, one way to do what you ask is to convert it to list (using list(iterator)), then use the usual list functions (len, slicing) to split it in half. Of course, in this way all the items will be in memory at the same time, which may or may not be acceptable in your case.

Alternatively, you can try to use a custom iterator class which keeps track of the total number of items that are going to be produced. Whether or not this is feasible depends on exactly how said iterators are obtained.

like image 153
Paolo Capriotti Avatar answered May 26 '23 20:05

Paolo Capriotti


Since the iterator protocol defines only two methods:

iterator.__iter__()

iterator.next()

the answer is no, in general case you can't know the number of items in a finite iterator without iterating through them.

like image 43
soulcheck Avatar answered May 26 '23 21:05

soulcheck