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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With