I am trying to reverse the same iterator several times, using the reversed function. For instance, with the code:
iterator = range(3)
N = 5
k = 0
out = []
while k < N:
for i in iterator:
out.append(i)
iterator = reversed(iterator)
k += 1
print(out)
I would want/expect to have the output:
[0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, ... ]
But, after the first use of reverse, I get the error:
TypeError: 'range_iterator' object is not reversible
What could be an efficient workaround that does not require unpacking the iterator?