list comprehension python 3 if else code example
Example 1: list comprehension python if else
[statement if condition else statement for _ in iterable_object]
Example 2: python list comprehension if else
[f(x) if condition(x) else '' for x in sequence]
Example 3: if else in list comprehension
[f(x) if condition else g(x) for x in sequence]
Example 4: list comprehension if
[f(x) for x in sequence if condition]
Example 5: else statement python list comprehension
>>> [a if a else 2 for a in [0,1,0,3]]
[2, 1, 2, 3]