Temporary variables in list comprehensions

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Sat Apr 8 01:51:52 EDT 2017


Roel Schroeven writes:

> Lele Gaifax schreef op 6/04/2017 20:07:
>> Piet van Oostrum <piet-l at pietvanoostrum.com> writes:
>>
>>> It is a poor man's 'let'. It would be nice if python had a real 'let'
>>> construction. Or for example:
>>>
>>> [(tmp, tmp + 1) for x in data with tmp = expensive_calculation(x)]
>>>
>>> Alas!
>>
>> It would be nice indeed!
>>
>> Or even
>>
>>   [(tmp, tmp + 1) for x in data
>>    with expensive_calculation(x) as tmp
>>    if tmp is not None]
>>
>
> Perhaps this:
>
>     [(tmp, tmp + 1) for tmp in
>      (expensive_calculation(x) for x in data)
>      if tmp is not None]
>
> A bit less elegant, but works right now.

The "poor man's let" works right now.



More information about the Python-list mailing list