For example:
a = [1,2,3]
x = [2*i for i in a]
y = [3*i for i in a]
Would it be more efficient to combine the list comprehensions into one (if possible) if the size of a is large? If so, how do you do this?
Something like,
x,y = [2*i, 3*i for i in a]
which doesn’t work. If using list comprehension is not more computationally efficient than using a normal for loop, let me know too. Thanks.
When in doubt about efficiency use the timeit module, it’s always easy to use:
The results seem to be consistent in pointing to the first option as the quickest.