from itertools import zip_longest


def grouper(n, iterable, fillvalue=None):
    """
    Split an iterable in groups of max N elements.
    grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    """
    args = [iter(iterable)] * n

    if fillvalue:
        return zip_longest(fillvalue=fillvalue, *args)

    for chunk in zip_longest(*args):
        yield filter(None, chunk)