Skip to content

Instantly share code, notes, and snippets.

@gyli
Last active April 20, 2019 20:09
Show Gist options
  • Save gyli/6658663b6047d1c65bb31dbb7f904583 to your computer and use it in GitHub Desktop.
Save gyli/6658663b6047d1c65bb31dbb7f904583 to your computer and use it in GitHub Desktop.
Slice an iterator into chunks
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