[Python-ideas] Accessing the result of comprehension's expression from the conditional

Calvin Spealman ironfroggy at gmail.com
Sat Jun 20 15:25:33 CEST 2009

On Sat, Jun 20, 2009 at 9:22 AM, MRAB<python at mrabarnett.plus.com> wrote:
> Ben Finney wrote:
>> Jim Jewett <jimjjewett at gmail.com> writes:
>>> On Fri, Jun 19, 2009 at 3:39 AM, Lie Ryan<lie.1296 at gmail.com> wrote:
>>>> res = [x**x as F for x in nums if F < 100]
>>> This, I have wanted.
>> You have it:
>>    res = [f for f in (x**x for x in nums) if f < 100]
>> In addition to the fact that this works now in existing Python, I find
>> it clearer than the above syntax you say you want.
> How about:
>   res = [F for x in nums with x**x as F if F < 100]
> :-)

That toggles the first part of the comprehensions to be or not be an
expression, depending on if there is a with clause later. You could
miss this when you read it, and it opens the door to doing more
strange things, like:

  res = [F/2 for x in nums with x**x as F if F < 100]

This is basically a strangely syntaxed nested loop

Read my blog! I depend on your acceptance of my opinion! I am interesting!
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy

More information about the Python-ideas mailing list