Asked By – Gerenuk
Can you think of a nice way (maybe with itertools) to split an iterator into chunks of given size?
chunks(l,3) becomes an iterator
[1,2,3], [4,5,6], 
I can think of a small program to do that but not a nice way with maybe itertools.
Now we will see solution for issue: Iterate an iterator by chunks (of n) in Python?
grouper() recipe from the
itertools documentation’s recipes comes close to what you want:
def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args)
It will fill up the last chunk with a fill value, though.
A less general solution that only works on sequences but does handle the last chunk as desired is
[my_list[i:i + chunk_size] for i in range(0, len(my_list), chunk_size)]
Finally, a solution that works on general iterators and behaves as desired is
def grouper(n, iterable): it = iter(iterable) while True: chunk = tuple(itertools.islice(it, n)) if not chunk: return yield chunk
This question is answered By – Sven Marnach
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0