def chunked(items, n):
    if n <= 0:
        raise ValueError("n must be greater than 0")
    items = iter(items)
    while True:
        chunk = []
        for _ in range(n):
            try:
                chunk.append(next(items))
            except StopIteration:
                break
        if chunk:
            yield chunk
        else:
            break
