[Python-ideas] Assignments in list/generator expressions

Bruce Leban bruce at leapyear.org
Sun Apr 10 02:42:48 CEST 2011


On Fri, Apr 8, 2011 at 9:22 PM, Mathias Panzenböck <
grosser.meister.morti at gmx.net> wrote:

> So I propose this syntax:
> >>> ys = *[f(x) as y for x in xs if y]*


On Sat, Apr 9, 2011 at 10:55 AM, MRAB <python at mrabarnett.plus.com> wrote:

> Or possibly:
>    ys =* [y for x in xs with f(x) as y if y]*
>

I find both of these hard to read, just like a sentence with multiple
non-parallel dependent phrases. The meaning of y as x is reversed from x in
y. As noted, there are two ways to do this that work already. I find the
first one quite readable:

On Fri, Apr 8, 2011 at 9:22 PM, Mathias Panzenböck <
grosser.meister.morti at gmx.net> wrote:

> Obviously there is a redundant function call. You could rephrase that code
> to the following in order to avoid it:
> >>> ys = *[y for y in (f(x) for x in xs) if y]*
>

On Sat, Apr 9, 2011 at 12:04 PM, Eugene Toder <eltoder at gmail.com> wrote:

> local assignment in list comprehension is easily emulated with for:

ys = *[y for x in xs for y in [f(x)] if y]*


I wonder if the "solution" to this is to publish one of these as the answer
to the common question of how to do this. Timing tests indicate that the
first version is faster.

--- Bruce
*New! *Puzzazz newsletter: http://j.mp/puzzazz-news-2011-04 including April
Fools!
*New!** *Blog post:
http://www.vroospeak.com/2011/04/march-gets-more-madness-next-year.html April
Fools!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110409/964783ab/attachment.html>


More information about the Python-ideas mailing list