Python List Comprehension With If/Else (Code Examples)
Use if else inside a Python list comprehension to transform elements, or a trailing if to filter them, with examples combining and nesting both.
A list comprehension supports if in two different places, and they do different jobs:
# if/else BEFORE the for — transforms every element
[x if x % 2 else 'even' for x in [1, 2, 3]]
# [1, 'even', 3]
# if AFTER the for — filters which elements are kept
[x for x in [1, 2, 3] if x % 2]
# [1, 3]
An if/else before the for keeps every element and chooses what each one becomes. An if after the for drops elements that fail the test—and it cannot take an else.
If list comprehension syntax itself is new to you, start with the basics of the inline for loop with if statements first.
If/else before the for: transform every element
The expression before the for can be a conditional expression (ternary operator) of the form value_if_true if condition else value_if_false:
>>> scores = [45, 82, 90, 58]
>>> ['pass' if score >= 60 else 'fail' for score in scores]
['fail', 'pass', 'pass', 'fail']
The new list always has the same number of elements as the original—each element is mapped to one of the two outcomes.
The else is mandatory here. Leaving it out is a syntax error, because a conditional expression must produce a value either way:
>>> ['pass' if score >= 60 for score in scores]
File "<stdin>", line 1
['pass' if score >= 60 for score in scores]
^^^
SyntaxError: expected 'else'
If after the for: filter elements
A condition placed after the for decides whether each element is included at all:
>>> scores = [45, 82, 90, 58]
>>> [score for score in scores if score >= 60]
[82, 90]
Here the new list can be shorter than the original—only elements satisfying the condition survive.
No else is allowed in this position. If you find yourself wanting one, you actually want the transforming form before the for:
>>> [score for score in scores if score >= 60 else 0]
File "<stdin>", line 1
[score for score in scores if score >= 60 else 0]
^^^^
SyntaxError: invalid syntax
Combine both: transform and filter
Both forms can appear in the same comprehension. The trailing if filters first, then the leading if/else transforms what remains:
>>> scores = [45, 82, 90, 58, None]
>>> ['pass' if score >= 60 else 'fail' for score in scores if score is not None]
['fail', 'pass', 'pass', 'fail']
The None entry is filtered out by the trailing condition, so the leading expression never has to handle it.
Multiple conditions
Chain trailing if clauses to require every condition (they behave like and):
>>> nums = [4, 9, 12, 15, 18]
>>> [n for n in nums if n % 2 == 0 if n > 10]
[12, 18]
Most code uses a single if with and instead, which reads more naturally:
>>> [n for n in nums if n % 2 == 0 and n > 10]
[12, 18]
For the transforming form, nest conditional expressions to handle three or more outcomes—but keep it short or move the logic into a function:
>>> scores = [45, 82, 95, 58]
>>> ['high' if s >= 90 else 'pass' if s >= 60 else 'fail' for s in scores]
['fail', 'pass', 'high', 'fail']
Nested conditional expressions evaluate left to right: the first true condition wins.
Quick reference
| Goal | Pattern | Result size |
|---|---|---|
| Transform every element | [a if c else b for x in items] | Same as input |
| Filter elements | [x for x in items if c] | Same or smaller |
| Filter then transform | [a if c1 else b for x in items if c2] | Same or smaller |
| Three or more outcomes | [a if c1 else b if c2 else d for x in items] | Same as input |
Remember the rule of thumb: if/else before the for changes what each element becomes; if after the for changes which elements are kept.
For more list comprehension patterns, see the inline for loop with if statements, for loop one-liners with if conditions and list comprehensions in reverse order.