I am working on a task that requires that I use an iterator multiple times. For example
#data
fruit= ("grape", "banana", "apple")
#iterator
myit = iter(fruit)
#the function I have
def printIter(its):
for x in its:
print(x)
def printIter2(its):
for x in its:
print(x)
I have to call printIter on the iterator twice but it is to perform completely different functions. But an iterator can only be consumed once.
I don't have control over the data source fruit and iterator myit. I only have control over the functions printIter().
How best can I achieve my aim using less memory.
What i currently have:
it1, it2 = itertools.tee(its)
printIter(it1)
printIter(it2)
del it1, it2
Is this a good practice, any other way?