more fun with iterators (mux, demux)

Miles semanticist at gmail.com
Wed Apr 8 17:12:56 EDT 2009


On Wed, Apr 8, 2009 at 1:21 PM, pataphor wrote:
> On Wed, 08 Apr 2009 10:51:19 -0400 Neal Becker wrote:
>
>> What was wrong with this one?
>>
>> def demux(iterable, n):
>>     return tuple(islice(it, i, None, n) for (i, it) in
>> enumerate(tee(iterable, n)))
>
> Nothing much, I only noticed after posting that this one handles
> infinite sequences too. For smallish values of n it is acceptable.

I assume that "smallish values of n" refers to the fact that
itertools.tee places items into every generator's internal deque,
which islice then skips over, whereas your version places items only
into the deque of the generator that needs it.  However, for small n,
the tee-based solution has the advantage of having most of the work
done in C instead of in Python generator functions; in my limited
benchmarking, the point where your version becomes faster is somewhere
around n=65.

-Miles



More information about the Python-list mailing list