[Python-ideas] Add 'interleave' function to itertools?
Oscar Benjamin
oscar.j.benjamin at gmail.com
Fri Aug 9 20:14:05 CEST 2013
On 9 August 2013 17:51, Andrew Barnert <abarnert at yahoo.com> wrote:
>> Similarly grouper() probably doesn't do what anyone really wants
>> (since it discards the end of the iterable).
>
> What do you mean by that? Every element in the original iterable ends up in one of the groups; nothing is discarded.
>
> I use this all the time. (Well, I often end up writing a "pairs" iterator by hand, and don't reach for grouper unless I already need it for different sized groups... But that's probably an argument _for_ putting it in the module, not against.)
Sorry, grouper as it stands does this:
>>> list(grouper(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, None, None)]
If you replace zip_longest with zip it does this:
>>> list(grouper(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
I've never had any use for either of those. The one I posted does this:
>>> list(grouper(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9,)]
The other one I've wanted is
>>> list(grouper(range(10), 3))
ValueError('Uneven chunksizes')
I consider the grouper() in the recipes to be the second-least useful
of the 4 possibilities (the least useful is the one that discards the
final part).
Oscar
More information about the Python-ideas
mailing list