I am creating an iterator using the iter
function and iterating several steps using the iterator.
I then want to continue iterating from the same place with a different iterator without affecting the original iterator object.
example:
consider the iterator object org_iter
:
>>> a = [1,2,3,4,5]
>>> org_iter = iter(a)
iterating over it:
>>> next(org_iter)
1
using iter
again gives the same instance of the iterator instead of a different one from the same location:
>>> new_iter = iter(org_iter)
>>> next(new_iter) # continuing
2
>>> next(org_iter) # "org_iter is new_iter"
3
EDIT because of comment: using itertools.tee
doesn't work either:
>>> new_iter = itertools.tee(org_iter)
>>> next(new_iter[0])
2
>>> next(org_iter)
3
the same functionality could be achieved by using different variables to hold the index numbers and +=1
ing them.
or with the enumerate
function and nested for
loops.
but i am specifically asking about using an iterator object directly.