create a function that splits a generator into chunks in python

To split a generator into chunks in Python, you can define a function that takes the generator and the chunk size as arguments. The function would yield each chunk until the generator is exhausted.
def split_generator(generator, chunk_size):
    chunk = []

    for item in generator:
        if len(chunk) == chunk_size:
            yield chunk
            chunk = []

    if len(chunk) > 0:
        yield chunk
242 chars
12 lines

You can then call this function with a generator and a chunk size to split it into chunks.

Here's an example of splitting a range generator into chunks of size 2:
range_generator = range(10)
chunk_size = 2

for chunk in split_generator(range_generator, chunk_size):
120 chars
6 lines

[0, 1]
[2, 3]
[4, 5]
[6, 7]
[8, 9]
35 chars
6 lines

In this example, the split_generator function returns each chunk of 2 items from the range_generator until the generator is exhausted.

gistlibby LogSnag