from collections.abc import Iterator, Sequence from typing import TypeVar T = TypeVar("T") def chunks(sequence: Sequence[T], chunk_size: int) -> Iterator[Sequence[T]]: """Yields slices of sequence object with size of slice limited by chunk_size argument """ for i in range(0, len(sequence), chunk_size): yield sequence[i : i + chunk_size]