Why chunks is not part of the python standard lib?

Serhiy Storchaka storchaka at gmail.com
Wed May 1 12:19:07 EDT 2013


01.05.13 09:26, Ricardo Azpeitia Pimentel написав(ла):
> After reading How do you split a list into evenly sized chunks in
> Python?
> <http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python>
> and seeing this kind of mistakes happening
> https://code.djangoproject.com/ticket/18972 all the time.
>
> Why is not a |chunks| function in itertools?

Because not every 1 line function needs to be in the standard library.

> |chunks([1,  2,  3,  4,  5],  3)
> # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.|

def chunks(seq, size):
     return [seq[i: i + size] for i in range(0, len(seq), size)]





More information about the Python-list mailing list