Python: if else in a list comprehension
Python's conditional expression is a if C else b
and can't be used as:
[a for i in items if C else b]
The right form is:
[a if C else b for i in items]
Even though there is a valid form:
[a for i in items if C]
But that isn't the same as that is how you filter by C
, but they can be combined:
[a if tC else b for i in items if fC]