I have a generator, let's say:
x = iter([1,2,3,4,5,6])
And if i want to loop 3 items at a time but step only 1 each time, i want to get:
1 2 3
2 3 4
3 4 5
5 6
I have tried looping two at a time but it steps 2 each time:
x = iter([1,2,3,4,5,6])
x = list(x)
for i,j in zip(x[::2],x[1::2]):
print i,j
[out]:
1 2
3 4
5 6
I have tried looping n at a time but it also steps n:
from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
x = iter([1,2,3,4,5,6])
for i,j in grouper(3,x):
print i,j
print
[out]:
Traceback (most recent call last):
File "test.py", line 11, in <module>
for i,j in grouper(3,x):
ValueError: too many values to unpack
I have tried just access +n each time i loop:
x = iter([1,2,3,4,5,6])
x = list(x)
for i,j in enumerate(x):
print x[i], x[i+1], x[i+2]
[out]:
1 2 3
2 3 4
3 4 5
4 5 6
5 6
Traceback (most recent call last):
File "test.py", line 26, in <module>
print x[i], x[i+1], x[i+2]
IndexError: list index out of range
Question:
Can I not change the generator into a list before looping and accessing more than one value at a time but stepping only 1 each time?
Other than using the last solution i have, how to else achieve the desired iteration?