[Python-ideas] Integrate some itertools into the Python syntax

Sven R. Kunze srkunze at mail.de
Wed Mar 23 17:55:32 EDT 2016



On 23.03.2016 22:44, Michael Selik wrote:
>
>
> On Wed, Mar 23, 2016, 5:27 PM João Bernardo <jbvsmo at gmail.com 
> <mailto:jbvsmo at gmail.com>> wrote:
>
>     On Mon, Mar 21, 2016 at 8:06 PM, Michel Desmoulin
>     <desmoulinmichel at gmail.com <mailto:desmoulinmichel at gmail.com>> wrote:
>
>         Itertools is great, and some functions in it are more used
>         than others:
>
>         - islice;
>         - chain;
>         - dropwhile, takewhile;
>
>
>     I like how dropwhile and takewhile could be easily integrated with
>     list comprehensions / generator expressions:
>
>     [x for x in range(10) while x < 5]      # takewhile
>     [x for x in range(10) not while x < 5]  # dropwhile
>     [x for x in range(10) from x >= 5]      # forward thinking dropwhile
>
>     I believe this is almost plain english without creating new keywords.
>
>
> They read well, except for the square brackets which to me imply 
> consuming the entire iterator. Itertools takewhile will early exit, 
> possibly leaving some values on the iterator. If these do consume the 
> entire iterator, what's the difference with the ``if`` clause in a 
> comprehension?

But isn't that their purpose? Not consuming. I think that would be easy 
to learn. +1 on this proposal.

This could also prove useful to avoid for-else. :-)

Best,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160323/c7ded7a1/attachment.html>


More information about the Python-ideas mailing list