Temporary variables in list comprehensions
roel at roelschroeven.net
Fri Apr 7 17:29:49 EDT 2017
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)]
> 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]
[(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.
More information about the Python-list