Skip to content

Instantly share code, notes, and snippets.

@rmporsch
Created June 23, 2021 05:27
Show Gist options
  • Save rmporsch/2efe69ddb38b6a6972a02c9aa57397f5 to your computer and use it in GitHub Desktop.
Save rmporsch/2efe69ddb38b6a6972a02c9aa57397f5 to your computer and use it in GitHub Desktop.
[Batching] #python
def batching(iterable: Sized, n: int = 1) -> Iterator:
"""
Batch a list or iterable
:param iterable:
:param n: items per batch
:return: iterator
"""
length = len(iterable)
for ndx in range(0, length, n):
yield iterable[ndx:min(ndx + n, length)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment