Created
January 16, 2023 19:50
-
-
Save rafalkrupinski/31fa1e9f2eeb6eb3ad3ca6698349efa2 to your computer and use it in GitHub Desktop.
Create chunks of given size out of an iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def iter_chunks(iter: Iterator[T], size: int) -> Iterator[Iterable[T]]: | |
"""Create chunks of given size out of an iterator""" | |
chunk: list[T] = [] | |
for idx, elem in enumerate(iter): | |
chunk.append(elem) | |
if idx + 1 % size == 0: | |
yield chunk | |
chunk = [] | |
if chunk: | |
yield chunk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment