Show the different ways to filter inside a Python comprehension, including choosing a value per item.
There are two filtering-looking constructs in a comprehension, and they do different jobs.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# An if clause filters: items that fail are left out entirely
evens = [n for n in numbers if n % 2 == 0]
print(evens) # [2, 4, 6, 8, 10]
# Filter AND transform
squares_of_evens = [n * n for n in numbers if n % 2 == 0]
print(squares_of_evens) # [4, 16, 36, 64, 100]
# A conditional expression changes the value, not the selection
labels = [n if n < 5 else -n for n in numbers]
print(labels) # [1, 2, 3, 4, -5, -6, -7, -8, -9, -10]
# More than one if means both must pass
print([n for n in numbers if n % 2 == 0 if n > 5]) # [6, 8, 10]The if clause sits after the for clause and drops items that do not satisfy it, so the result is shorter than the input. The conditional expression sits before the for clause and runs for every item, choosing one of two values, so the result is the same length as the input. That is the distinction to be able to state clearly.
You can use both together — filter first, then transform the survivors — which is what the squares_of_evens line does. The order in the comprehension is fixed by the syntax: expression, then for clause, then if clauses. The if clauses are evaluated before the expression, so the expression never sees the items that were filtered out.
Two more clauses can be chained, and both must pass. The last line keeps the even numbers that are also greater than 5, giving 6, 8 and 10. There is no or available, so a disjunction needs a single condition built with or — n % 2 == 0 or n % 3 == 0 — which is the usual workaround.