The following code works in Python
var=range(20)
var_even = [0 if x%2==0 else x for x in var]
print var,var_even
However, I thought that the conditions need to be put in the end of a list. If I make the code
var_even = [0 if x%2==0 for x in var]
Then it won't work. Is there a reason for this?
ifcondition in your list comprehension (not withelse) you need to put it afterforif you want to use it with else you should put them beforefor.else. See the docs.(0 if x%2 == 0 else x)is a conditional expression. It is not part of the list comprehension.