def chunked(items, n):
    if n <= 0:
        raise ValueError("n must be > 0")
    chunk = []
    for item in items:
        chunk.append(item)
        if len(chunk) == n:
            yield chunk
            chunk = []
    if chunk:
        yield chunk
