Sequence splitting

Terry Reedy tjreedy at
Fri Jul 3 15:23:47 EDT 2009

Brad wrote:
> On Jul 2, 8:17 pm, "Pablo Torres N." < at> wrote:
>> This sounds like it belongs to the python-ideas list.  I suggest
>> posting there for better feedback, since the core developers check
>> that list more often than this one.
> I tried posting on python-ideas and received a "You are not allowed to
> post to this mailing list" reply. Perhaps because I am posting through
> Google groups? Or maybe one must be an approved member to post?

Spammers post thru Google groups

Either subscribe or access via as gmane.comp.python.ideas.

As to your main question: this was discuss some years ago. There did not 
seem enough use cases for 'bifilter' or 'distributor' to warrent 
inclusion in the stdlib. Easy enough to do on one's own for those few 
cases. Just filter twice.

Usually, one wants to do one thing or another to each item with the 
original scan

for item in collection:
   if pred(item):

Having proc_true and proc_false both be append(item) is a special case.

Note that filter has been changed from returning a list to returning an 
iterator.  So a proposal for a function to return two lists would seem 
backwards. A function that returns two iterators would be possible. 
Something like itertools.tee except that each item would go on one tee 
or the other instead of both. It would have two queues (deques) instead 
of one. And it should be lazy -- items should only be pulled from the 
original iterator when an item is requested from an empty queue.

I am not sure it is worth it. In the worst case, all items are say 
'True' and you request 'False' items and all items get copied to the 
True queue before the False iterator raised StopIteration.

The way to avoid calling keyfunc(item) twice on each item is to make a 
new list first: newlist = list(map(keyfunc,iterable)). Then scan newlist 

If you write such a function, first in Python, then C, you can register 
code on PyPI and see how much interest it gets.

Terry Jan Reedy

More information about the Python-list mailing list