Skip to content

Instantly share code, notes, and snippets.

@rkhullar
Created April 4, 2023 14:33
Show Gist options
  • Save rkhullar/637e0641faae5e74624073210f5853cc to your computer and use it in GitHub Desktop.
Save rkhullar/637e0641faae5e74624073210f5853cc to your computer and use it in GitHub Desktop.
python iter batch v1
from typing import Iterator, List, TypeVar
T = TypeVar('T')
def iter_batch(sequence: Iterator[T], size: int) -> Iterator[List[T]]:
count, buffer = 0, [None] * size
for item in sequence:
idx = count % size
buffer[idx] = item
if idx + 1 == size:
yield buffer
count += 1
remaining = count % size
if remaining > 0:
yield buffer[: remaining]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment