Search code examples
pythongeneratorpython-itertools

Chunking a generator


What am I doing wrong here. Trying to get chunks of a generator using islice, but seems to be running infinitely.

from itertools import islice

size = 2

def g():
    for x in range(11):
        print("generating: ", x)
        yield x

while True:
    chunk = islice(g(), size)
    if not chunk:
        break
    print("at chunk")
    for c in chunk:
        print(c)

I am getting an output of, that seems to just loop forever and does not seem to increment:

at chunk
generating:  0
0
generating:  1
1
at chunk
generating:  0
0
generating:  1
1
at chunk
generating:  0
0
generating:  1
1
at chunk

Solution

  • Each time you call g() you restart the generator from the beginning. You need to assign the result to a variable so it will keep using the same generator.

    And as mentioned in a comment, the islice object is always truthy. To tell if you reached the end, check whether the for c in chunk: loop did anything.

    from itertools import islice
    
    def g():
        for x in range(11):
            print("generating: ", x)
            yield x
    
    size = 2
    gen = g()
    while True:
        chunk = islice(gen, size)
    
        print("at chunk")
        empty = True
        for c in chunk:
            print(c)
            empty = False
    
        if empty:
            break