I am able to successfully execute the below conditional expression
[i if i%2 != 0 else None for i in range(10)]
Output:
[None, 1, None, 3, None, 5, None, 7, None, 9]
However, I don't want to add None
and keep only the odd numbers
in the list.
To achieve that, I am executing the below code:
[i if i%2 != 0 else pass for i in range(10)]
The above statement is throwing an error:
SyntaxError: invalid syntax
What is the problem here with using pass
? Why pass
cannot be the operand of a ternary operator
?
Aucun commentaire:
Enregistrer un commentaire