I am trying to use a list comprehension to extract specific elements from a list, using conditionals on the list indices.
When the list indices differ, specific operations need to happen.
When the list indices are the same, no element should be added.
The latter is what I do not know how to do, except by adding '' and removing it afterwards.
Example (simpler than my actual case, but conceptually the same):
x = [0, 1, 2, 3, 4]
i = 2
x2 = [2 * x[j] - x[i] if j > i else 2 * x[i] - x[j] if j < i else '' for j in x]
x2.remove('')
x2
# [4, 3, 4, 6]
How would you exclude the case where i == j a priori?
I would have thought that just not having else '' at the end would work, but then I get an invalid_syntax error.
I suppose in essence I am looking for a neutral element for the list comprehension.