I am trying to translate this for loop into a list comprehension:
a = [1,2,3,4,5,6,7,8,9]
result = []
for i in a:
if i <= 3:
result.append(1)
elif i > 4 and i < 7:
result.append(2)
and I have tried this
[1 if i <= 3 else 2 if i > 3 and i < 7 for i in a]
which complains about
File "<ipython-input-155-eebf07a9e0d8>", line 2
[1 if i <= 3 else 2 if i > 3 and i < 7 for i in a]
^
SyntaxError: invalid syntax