Skip to main content
new solution based on input from OP
Source Link
pastaleg
  • 1.8k
  • 2
  • 18
  • 25

Since iterators are stateful and its resources consumed I am not sure what the goal of using the same iterator twice would be.

However, if you dont want to have the memory overhead of two copies of the iterator at the same time as is happening with tee(), you can just redeclare the iterator after the first is consumed and deleted

import itertools

#the function I have
def printIter(its):
  for x in its:
    print(x)

def printIter2(its):
  for x in its:
    print(x)


#data
fruit= ("grape", "banana", "apple")

#iterator
myit = iter(fruit)

#it1, it2 = itertools.tee(myit)
printIter(myit)
del myit
myit = iter(fruit)
printIter2(myit)
del myit

Since you indicated that you have no access to the original data, tee() is probably the best you can do with iterators. However, you can consider converting the single iterator to a list, then do the repetitive operations on that.

import itertools
#data
fruit= ("grape", "banana", "apple")
#iterator
myit = iter(fruit)

def printIter(its):
  for x in its:
    print(x)

mylist = list(myit)
del myit
printIter(mylist)
printIter(mylist)

Since iterators are stateful and its resources consumed I am not sure what the goal of using the same iterator twice would be.

However, if you dont want to have the memory overhead of two copies of the iterator at the same time as is happening with tee(), you can just redeclare the iterator after the first is consumed and deleted

import itertools

#the function I have
def printIter(its):
  for x in its:
    print(x)

def printIter2(its):
  for x in its:
    print(x)


#data
fruit= ("grape", "banana", "apple")

#iterator
myit = iter(fruit)

#it1, it2 = itertools.tee(myit)
printIter(myit)
del myit
myit = iter(fruit)
printIter2(myit)
del myit

Since iterators are stateful and its resources consumed I am not sure what the goal of using the same iterator twice would be.

However, if you dont want to have the memory overhead of two copies of the iterator at the same time as is happening with tee(), you can just redeclare the iterator after the first is consumed and deleted

import itertools

#the function I have
def printIter(its):
  for x in its:
    print(x)

def printIter2(its):
  for x in its:
    print(x)


#data
fruit= ("grape", "banana", "apple")

#iterator
myit = iter(fruit)

#it1, it2 = itertools.tee(myit)
printIter(myit)
del myit
myit = iter(fruit)
printIter2(myit)
del myit

Since you indicated that you have no access to the original data, tee() is probably the best you can do with iterators. However, you can consider converting the single iterator to a list, then do the repetitive operations on that.

import itertools
#data
fruit= ("grape", "banana", "apple")
#iterator
myit = iter(fruit)

def printIter(its):
  for x in its:
    print(x)

mylist = list(myit)
del myit
printIter(mylist)
printIter(mylist)
Source Link
pastaleg
  • 1.8k
  • 2
  • 18
  • 25

Since iterators are stateful and its resources consumed I am not sure what the goal of using the same iterator twice would be.

However, if you dont want to have the memory overhead of two copies of the iterator at the same time as is happening with tee(), you can just redeclare the iterator after the first is consumed and deleted

import itertools

#the function I have
def printIter(its):
  for x in its:
    print(x)

def printIter2(its):
  for x in its:
    print(x)


#data
fruit= ("grape", "banana", "apple")

#iterator
myit = iter(fruit)

#it1, it2 = itertools.tee(myit)
printIter(myit)
del myit
myit = iter(fruit)
printIter2(myit)
del myit