import itertools


def grouper(iterable, n, fillvalue=None):
    """Collect data into fixed-length chunks or blocks

    Parameters
    ----------
    iterable: iterable
        an iterable to group by batches
    n: int
        the number of elements of each batch
    fillvalue: object
        the value for filling the last batch if the iterable length is not a multiple of n
    """
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    # this code is copied from itertools recipes
    args = [iter(iterable)] * n
    return itertools.zip_longest(fillvalue=fillvalue, *args)