1

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

kharandziuk
  • 12,020
  • 17
  • 63
  • 121
  • Loop over it and get the next item using `next()`. Though you won't be able to do `len() % 2 == 0` without actually consuming it. – Ashwini Chaudhary Nov 09 '15 at 14:38
  • You won't be able to find out if there's an even number until you reach the end, but if you search for *"consume iterator in pairs"* I'm sure you'll find something. – jonrsharpe Nov 09 '15 at 14:39
  • [See here](http://stackoverflow.com/a/5389547/189205) – interjay Nov 09 '15 at 14:44

2 Answers2

3

You could do something like:

In [103]: def lst():
    i = 1
    while i <= 10:
        yield i
        i += 1

In [104]: g = lst()

In [105]: while True:
    try:
        v1, v2 = next(g), next(g)
        print(v1,v2)
    except StopIteration:
        break
   .....:     
(1, 2)
(3, 4)
(5, 6)
(7, 8)
(9, 10)
xnx
  • 24,509
  • 11
  • 70
  • 109
1

Using the lst()) function from xnx's reply to create the generator:

def lst():
    i = 1
    while i <= 10:
        yield i
        i += 1
gen = lst()

A generalized solution using itertools

from itertools import islice

def slice_generator(gen, number_of_elements = 2):
    result = []

    while 1:
        l  = tuple(itertools.islice(gen, number_of_elements))
        if not l:
            return result
        result.append(l)
        print l 

>>> my_list = slice_generator(gen, 3)
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)
(10,)

>>> print my_list
[(1, 2, 3), (4, 5, 6), (7, 8, 9), (10,)]
Boa
  • 2,609
  • 1
  • 23
  • 38