if/else in a list comprehension

You can totally do that. It’s just an ordering issue: In general, And, for list comprehensions with if conditions only, Note that this actually uses a different language construct, a conditional expression, which itself is not part of the comprehension syntax, while the if after the for…in is part of list comprehensions and used to filter elements from the source iterable. Conditional expressions can be … Read more

if/else in a list comprehension

You can totally do that. It’s just an ordering issue: In general, And, for list comprehensions with if conditions only, Note that this actually uses a different language construct, a conditional expression, which itself is not part of the comprehension syntax, while the if after the for…in is part of list comprehensions and used to … Read more