Is there a way to output two (or more) elements for each iteration in a list / dictionary / set view? As a simple example, to output all positive and negative doublings of integers from 1 to 3 (i.e. {x | x = ±2n, n ∈ {1...3}}) is there a syntax like the following?
>>> [2*i, -2*i for i in range(1, 4)]
[2, -2, 4, -4, 6, -6]
I know that I can output tuples (+i,-i)and smooth it out, but I was wondering if there is a way to completely solve the problem using one understanding.
I am currently creating two lists and merging them (which works if the order is not important):
>>> [2*i for i in range(1, 4)] + [-2*i for i in range(1, 4)]
[2, 4, 6, -2, -4, -6]
source
share