On Sat, Apr 6, 2013 at 2:53 PM, Giampaolo Rodolà <g.rodola@gmail.com> wrote:
2013/4/6 João Bernardo <jbvsmo@gmail.com>:
> Isn't it just
>
>    divmod(total, step)
>
> João Bernardo

Not really:

>>> list(chunks(13, 4))
[4, 4, 4, 1]
>>> divmod(13, 4)
(3, 1)


I think what João means is you can do:

def chunks(total, step):
    a,b = divmod(total,step)
    return [step]*a + [b]

>>> chunks(13,4)
[4, 4, 4, 1]

Or, to avoid necessarily constructing the list all at once:

def chunks(total, step):
    a,b = divmod(total,step)
    return itertools.chain(itertools.repeat(step,a), [b])

>>> list(chunks(13,4))
[4, 4, 4, 1]

Nathan
 
Literally chunks() keeps yielding 'step' until 'total' is reached and
makes sure the last yielded item has the correct remainder.