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.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to