Proposal: [... for ... while cond(x)]
Eighty
eightyx at gmail.com
Sun Aug 6 13:46:23 EDT 2006
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 would this syntax offer that:
>
> [x for x in takewhile(cond, xs)]
>
> doesn't currently offer? (Apart, that is, from saving you 3 characters of
> typing)
The same thing that [f(x) for x in xs] offers that map(f, xs) doesn't,
and the same thing that [x for x in xs if f(x)] offers that filter(f,
xs) doesn't. It's more "pythonic". You can use an expression for cond
instead of a lambda.
More information about the Python-list
mailing list