Last active
April 20, 2019 20:09
-
-
Save gyli/6658663b6047d1c65bb31dbb7f904583 to your computer and use it in GitHub Desktop.
Slice an iterator into chunks
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
from itertools import islice | |
from typing import List, Iterator | |
def chunks(iterator: Iterator, step: int) -> List: | |
""" | |
Yield successive step-sized chunks from iterator | |
""" | |
iterable = iter(iterable) | |
while True: | |
lines = list(islice(i, step)) | |
if not lines: | |
return | |
yield lines | |
# Another approach | |
# iterable = iter(iterable) | |
# yield from iter(lambda: list(islice(iterable, step)), []) | |
chunks([1,2,3,4,5], 2) | |
chunks(range(5), 2) | |
chunks('abcde', 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment