Filtering in a list comprehension is one of the most Pythonic and efficient ways to create a new list containing only elements from an existing iterable that satisfy a condition — all in a single, readable expression. The syntax [expr for item in iterable if condition] combines iteration, transformation, and filtering, avoiding explicit loops and temporary lists. In 2026, list comprehensions with filtering remain essential — they’re concise, fast (optimized in CPython), memory-efficient for small-to-medium data, and widely used in data cleaning, feature selection, and preprocessing in pandas/Polars pipelines, ML workflows, and production scripts.
Here’s a complete, practical guide to filtering in list comprehensions in Python: basic syntax, multiple conditions, nested comprehensions, real-world patterns, performance tips, and modern best practices with type hints, generator expressions, Polars/pandas equivalents, and alternatives for large data.
Basic filtering — keep only items matching the condition; expression can transform the item.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Filter even numbers (keep as-is)
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers) # [2, 4, 6, 8, 10]
# Filter and transform: squares of even numbers > 20
even_squares_big = [num**2 for num in numbers if num % 2 == 0 and num**2 > 20]
print(even_squares_big) # [36, 64, 100]
Multiple conditions — chain with and/or; use parentheses for clarity.
values = [-5, 10, -15, 20, 25, -30, 35]
# Positive numbers between 10 and 30 inclusive
positive_mid = [v for v in values if v > 0 and 10 <= v <= 30]
print(positive_mid) # [10, 20, 25]
# Alternative: use 'and' or multiple 'if' (nested comprehension style)
positive_mid_nested = [v for v in values if v > 0 if 10 <= v <= 30]
print(positive_mid_nested) # same result
Real-world pattern: filtering pandas/Polars data in list comprehensions — clean lists before DataFrame creation or post-processing.
import pandas as pd
# Raw data: list of dicts from API or file
raw_data = [
{'id': 1, 'value': 100, 'category': 'A'},
{'id': 2, 'value': -50, 'category': 'B'},
{'id': 3, 'value': 200, 'category': 'A'},
{'id': 4, 'value': 150, 'category': 'C'}
]
# Filter valid positive A-category records
clean_records = [
record for record in raw_data
if record['value'] > 0 and record['category'] == 'A'
]
df = pd.DataFrame(clean_records)
print(df)
# id value category
# 0 1 100 A
# 1 3 200 A
Best practices make filtered list comprehensions safe, readable, and performant. Keep comprehensions short and readable — if logic gets complex, use a regular loop or named function. Modern tip: for large data, prefer generator expressions (...) or Polars lazy filtering — pl.DataFrame(raw_data).filter(...) avoids full list allocation. Use if clauses wisely — multiple if acts like nested loops (cartesian), not chained conditions. Prefer boolean indexing on pandas/Polars — df[df['value'] > 0] — faster and clearer than list comps on DataFrames. Add type hints — def filter_positive(data: list[dict]) -> list[dict]. Avoid side effects inside comprehensions — keep pure (no prints, no mutable changes). Use filter() + map() — for functional style, but comprehensions are usually faster/readable. Profile memory — sys.getsizeof([expr for ...]) vs generator. Use itertools — filterfalse, takewhile for advanced filtering. Prefer Polars for big data — pl.scan_csv(...).filter(...).collect() — lazy, columnar, faster. Combine with pd.read_csv(chunksize=...) — filter chunks, concat results. Test edge cases — empty input, all filtered out, mixed types.
Filtering in list comprehensions creates new lists with only matching/transformed elements — concise, fast, Pythonic. In 2026, keep them short, prefer generator expressions for laziness, use pandas/Polars boolean filtering for DataFrames, and profile memory for large data. Master filtered comprehensions, and you’ll write clean, efficient code for data selection, cleaning, and transformation.
Next time you need a filtered list — use a comprehension. It’s Python’s cleanest way to say: “Give me only the items that match — in one readable line.”