Temporary variables in list comprehensions
Serhiy Storchaka
storchaka at gmail.com
Tue Jan 10 02:19:14 EST 2017
On 09.01.17 12:46, Paul Rubin wrote:
> Serhiy Storchaka <storchaka at gmail.com> writes:
>> gen = (expensive_calculation(x) for x in data)
>> result = [(tmp, tmp + 1) for tmp in gen]
>
> result = [(tmp, tmp+1) for tmp in map(expensive_calculation, data)]
Yes, of course, but only in the case of one-argument function
expensive_calculation(). If this is just an expensive expression or need
to pass multiple arguments to an expensive function, a generator
expression could look simpler than a lambda or a local function.
More information about the Python-list
mailing list