[Python-ideas] Add 'interleave' function to itertools?

Tim Peters tim.peters at gmail.com
Thu Aug 8 20:48:23 CEST 2013


[David Mertz]
>> I agree that most of the recipes for clever combinations of itertools
>> functions--at least those that have intuitive and obvious names--should
>> simply be put into the module itself (and per Andrew, they can nonetheless
>> by documented as recipes).
>>
>> Even if these implementations can be done as one-liners that stitch together
>> a few existing functions, that's not really any reason not to include them
>> in the namespace.  There are too many ways to do those one-liners slightly
>> wrong, and no need to add to the cognitive burden of someone who just wants
>> to, e.g. interleave.

[Guido]
> I'm not sure if I completely buy this line of argument. Coming up with
> good names for all of them will become an issue, and people who can
> easily reconstruct the recipes from first principles won't necessarily
> be using them -- they may even have to look up the recipe by name
> before they understand what is going on.
>
> (Then again, feel free to ignore me. I rarely use the itertools module anyway.)

That's the thing:  not everyone who could benefit from these "trivial"
functions has an itertools view of the world; I'd wager that _most_
people are not among those "who can easily reconstruct the recipes
from first principles".  Canning simple operations is primarily for
their benefit, not for itertools experts.

For example, there's a long enhancement request about adding a
"first_true" function, returning the first item in its iterable
argument that evaluates to a true value (with an optional `pred`
argument to define truthfulness, and an optional default value if no
iterate "is true"):

    http://bugs.python.org/issue18652

It's an easy one-liner to an itertools expert, but the history in the
bug report shows that the author has seen wide adoption of his package
supplying this function.  The typical non-expert codes a loop, and
typically gets some end cases wrong (see the bug report for
embarrassing examples - LOL ;-) ).

To the non-expert, which itertools-like functionalities are "core" -
which should be derived from which others - is a mystery.  So I'm with
David on throwing them all in, "at least those that have intuitive and
obvious names".  Let a thousand redundancies bloom ;-)


More information about the Python-ideas mailing list