With a list I can do something like this:
lst = [1, 2, 3, 4]
assert len(lst) % 2 == 0 # I know that source has n % 2 == 0 elements
for i in range(0, len(lst), 2):
print lst[i] + lst[i+1]
How can I achieve the same behavior but in case when lst
is a generator? I understand that can take a "length" of generator without consuming it.
Now I finished with code below:
for l in gen:
r = next(gen)
print l + r
But it doesn't work for n-case