I am trying to get a grasp of list comprehension and ran into a problem that throws a syntax error.
Here I'm trying to get a list of odd number:
ll = []
for each in l:
if each%2 == 1:
ll.append(each)
else:
pass
ll
>>> [1, 3, 5]
Using list comprehension, however, this throws a syntax error at pass
:
l = [1,2,3,4,5]
[each if each%2==1 else pass for each in l]
>>> [each if each%2==1 else pass for each in l]
^
>>> SyntaxError: invalid syntax
If I were to replace pass with something like 0, it would work and return [1,0,3,0,5] without throwing an error. Could someone explain why I can't use pass here?