SyntaxError: invalid syntax in inline function


I am trying to perform one program with the list comprehension Python. But it is showing the syntax error.

a = [1,2,3]
b = [i*2 for i in a if i>0 else 0 ]
print(b)

1 Answer

4 years ago by

You need to write the for loop after if else statement as shown below

a = [1,2,3]
b = [i*2 if i>0 else 0 for i in a ]
print(b)

check output here

4 years ago by Divya