Chunked Iteration in Python

from blog Alex W.'s Blog, | ↗ original
from typings import Generator, Iterable, TypeVar ChunkT = TypeVar("ChunkT") def chunks( iterable: Iterable[ChunkT], size: int ) -> Generator[Iterable[ChunkT], None, None]: """ Break *iterable* into lists of length *n*: >>> list(chunked([1, 2, 3, 4, 5, 6], 3)) [[1, 2, 3], [4, 5, 6]] The last yielded list will have fewer than *n* elements if the...