Assignments in python list comprehension
I am looking for a way to do assignments in a list comprehension. I would like to rewrite something like the following piece of code into a list comprehension.
I have this "costly" function:
import time
def f(x):
time.sleep(1)
return x + 1
And this loop:
l = []
for value in [1, 2, 3]:
x = f(value)
l.append(x + x)
I would like to rewrite this to a list comprehension:
l = [
f(value) + f(fvalue)
for value in [1, 2, 3]
]
But since calling f(value)
is costly, I would like to minimize the number of calls (this following snippet doesn't run):
l = [
(x = f(value))
x + x
for value in [1, 2, 3]
]
I've read about the assignment expression (:=
) (https://www.python.org/dev/peps/pep-0572/#changing-the-scope-rules-for-comprehensions) but I can't seem to figure it out.
My approach would be to nest multiple list comprehension, like
l_new = [x * x * y for x, y in [f(value) for value in [1, 2, 3]]]
So f()
should only be called once for each value.