I suggest a new extension of the list comprehension syntax:
[x for x in xs while cond(x)]
which would be equivalent to
list(itertools.takewhile(cond, xs))
+ Since Python favors list comprehensions over map, filter, and reduce,
this would be the preferred way to do this
+ "Takewhile operations"
Duncan Booth wrote:
> Eighty wrote:
>
> > I suggest a new extension of the list comprehension syntax:
> >
> > [x for x in xs while cond(x)]
> >
> > which would be equivalent to
> >
> > list(itertools.takewhile(cond, xs))
> >
>
> What wou
Eighty wrote:
> I suggest a new extension of the list comprehension syntax:
>
> [x for x in xs while cond(x)]
>
> which would be equivalent to
>
> list(itertools.takewhile(cond, xs))
>
> + Since Python favors list comprehensions over map, filter, and reduce,
> this
Terry Reedy wrote:
> whereas the analogous expansion of your proposal
>
> for x in xs:
> while cond(x):
> yield e(x)
>
> is an infinite loop and not at all what you mean.
You're right. The syntax is ambiguous. I agree it's not a good idea,
now. :)
> x for x in xs while cond(x) if blah(x)