avatar Bite 222. Split an iterable in groups of size n

In this Bite you will complete the group function that receives an iterable and splits it up in n groups. Say we have a list of 10 ints and n=3, passing this into the function you'd get the following return:

>>> iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> n = 3
>>> from grouping import group
>>> group(iterable, n)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

It should also work passing in a generator:

>>> iterable = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> group(iterable, n)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

And of course for different values for iterable and n (see also the tests):

>>> iterable = [1, 2, 3, 4] * 3
>>> iterable
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> group(iterable, 2)
[[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]]

Thanks to Andrew (CodeItch) for letting us know about this code/idea derived from one of Sumo Logic's repos.

Have fun and keep calm and code in Python! See you in the next Bite ...

Login and get coding
go back Intermediate level
Bitecoin 3X

119 out of 125 users completed this Bite.
Will you be Pythonista #120 to crack this Bite?
Resolution time: ~34 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 3.0 on a 1-10 difficulty scale.
» You can do it! 😌

We use Python 3.8