def grouper(iterable, n):
    """Collect data into chunks of at most n elements"""
    assert n > 0, 'Cannot group into chunks of zero elements'
    lst = []
    iterable = iter(iterable)
    while True:
        try:
            lst.append(next(iterable))
        except StopIteration:
            break
        if len(lst) == n:
            yield lst
            lst = []
    if lst:
        yield lst
