Follow

oof forgot python had no chunks() and you have to import three lines from stackoverflow

a module called stackoverflow with all those short snippets in it

@CobaltVelvet oh, I didn't know that was a thing in Rust, and it's something I frequently wish I had when I do something with Python, cool

@melunaka pour passer d'un itérateur plat à un itérateur de groupes de N éléments

@CobaltVelvet Three lines? Excuse me but:

>>> l = list(range(20))
>>> list(itertools.zip_longest(*(l[i::5] for i in range(5))))
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14), (15, 16, 17, 18, 19)]

@CobaltVelvet
Ooooh, even better:

>>> list(itertools.zip_longest(*[iter(l)]*5))
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14), (15, 16, 17, 18, 19)]

(ctrl-f "def grouper" in docs.python.org/3/library/iter )

@CobaltVelvet But I agree, this should be part of itertools already

Sign in to participate in the conversation
Octodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!