I am using a function in a list comprehension and an if function:
new_list = [f(x) for x in old_list if f(x) !=0]
It annoys me that the expression f(x)
is computed twice in each loop.
Is there a way to do it in a cleaner way? Something along the lines of storing the value or including the if statement at the beginning of the list comprehension.