RussianPenguin
5/26/2019 - 3:29 PM

Iterate by chunk

General solution for splitting any general iterator by chunks.

import itertools

def grouper(n, iterable):
    it = iter(iterable)
    while True:
       chunk = tuple(itertools.islice(it, n))
       if not chunk:
           return
       yield chunk