It’s because data
is an iterator, and you can consume an iterator only once. For example:
lst = [1, 2, 3]
it = iter(lst)
next(it)
=> 1
next(it)
=> 2
next(it)
=> 3
next(it)
=> StopIteration
If we are traversing some data using a for
loop, that last StopIteration
will cause it to exit the first time. If we try to iterate over it again, we’ll keep getting the StopIteration
exception, because the iterator has already been consumed.
Now for the second question: What if we do need to traverse the iterator more than once? A simple solution would be to save all the elements to a list, which can be traversed as many times as needed. For instance, if data
is an iterator:
data = list(data)
That is alright as long as there are few elements in the list. However, if there are many elements, it’s a better idea to create independent iterators using tee()
:
import itertools
it1, it2 = itertools.tee(data, 2) # create as many as needed
Now we can loop over each one in turn:
for e in it1:
print("doing this one time")
for e in it2:
print("doing this two times")