def grouper(iterable, n, fillvalue=None):
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    while True:
        l = []
        try:
            for x in args:
                l.append(next(x))
        except:
            pass
        yield l
        if len(l) < n:
            break
Collect data into fixed-length chunks or blocks
dataset/ETHPy150Open openworm/PyOpenWorm/PyOpenWorm/data.py/grouper