I am trying to make an iterator that goes "backwards" too. Itertools cycle provides a similar function cycle(range(3)) and the next function produces 0,1,2,0,1,2. However, I would like 0,1,2,1,0,1,2,1,0,1,2....
I tried using iter tools cycle. Here is the code.
from itertools import cycle
myIterator = cycle(range(3))
print(next(myIterator))
print(next(myIterator))
print(next(myIterator))
print(next(myIterator))
Output: 0 1 2 0
Just pass the sequence you want to cycle to
cycle:For an arbitrary list, you can use
which outputs