Login and get codingIn this Bite you will complete the
group
function that receives aniterable
and splits it up inn
groups. Say we have a list of 10int
s andn=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
andn
(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 ...
145 out of 153 users completed this Bite.
Will you be the 146th person to crack this Bite?
Resolution time: ~36 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 3.0 on a 1-10 difficulty scale.
» You can do it! 😌