Sequence splitting

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Jul 3 04:26:59 EDT 2009


On Fri, 03 Jul 2009 01:02:56 -0700, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>> I've never needed such a split function, and I don't like the name, and
>> the functionality isn't general enough. I'd prefer something which
>> splits the input sequence into as many sublists as necessary, according
>> to the output of the key function. Something like itertools.groupby(),
>> except it runs through the entire sequence and collates all the
>> elements with identical keys.
> 
> No really, groupby makes iterators, not lists, and it you have to
> develop quite a delicate sense of when you can use it without having
> bugs caused by the different iterators it makes getting advanced at the
> wrong times.  The concept of a split function that actually works on
> lists is useful.  I'm neutral about whether it's worth having a C
> version in the stdlib.

groupby() works on lists.

The difference between what I'm suggesting and what groupby() does is 
that my suggestion would collate *all* the elements with the same key, 
not just runs of them. This (as far as I can tell) requires returning 
lists rather than iterators.

The most important difference between my suggestion and that of the OP is 
that he limited the key function to something which returns a truth 
value, while I'm looking for something more general which can split the 
input into an arbitrary number of collated sublists.



-- 
Steven



More information about the Python-list mailing list