Conditionals in Generator Expressions – Memory-Efficient Filtering 2026
Adding conditionals to generator expressions combines the power of filtering with lazy evaluation. This pattern is extremely useful in data science when you need to process large datasets while keeping memory usage minimal.
TL;DR — Two Ways to Add Conditions
- Filtering:
(expr for item in iterable if condition) - Conditional Value:
(expr_if_true if condition else expr_if_false for item in iterable)
1. Basic Filtering with if
scores = [85, 92, 78, 95, 88, 76, 91, 65]
# Generator that yields only high scores
high_scores = (score for score in scores if score >= 90)
for score in high_scores:
print(score)
2. Conditional Expressions (if-else) in Generators
scores = [85, 92, 78, 95, 65, 45]
# Categorize on-the-fly without creating a full list
categories = (
"High" if s >= 90
else "Medium" if s >= 75
else "Low"
for s in scores
)
for cat in categories:
print(cat)
3. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("large_sales_data.csv")
# Example 1: Memory-efficient high-value transaction filter
high_value_revenue = sum(
row.amount * 1.1
for row in df.itertuples()
if row.amount > 1500 and row.region == "North"
)
# Example 2: Lazy feature engineering with conditions
processed_amounts = (
0 if amount <= 0
else round(amount ** 0.5, 2)
for amount in df["amount"]
if pd.notna(amount)
)
# Example 3: Streaming customer categorization
premium_customers = (
row.customer_id
for row in df.itertuples()
if row.amount > 2000
and row.region in ("North", "West")
)
for cust_id in premium_customers:
print(f"Premium customer: {cust_id}")
4. Best Practices in 2026
- Use filtering
ifat the end for selecting rows - Use
if-elseinside for value transformation - Combine with
sum(),max(), or a singleforloop to keep memory low - Avoid deeply nested conditionals — they reduce readability
- For very complex logic, use a traditional generator function with
yield
Conclusion
Conditionals in generator expressions give you the best of both worlds: clean, readable filtering/transformation combined with excellent memory efficiency. In 2026 data science workflows, this pattern is preferred when processing large files, streaming data, or building memory-conscious pipelines.
Next steps:
- Replace list comprehensions that only get iterated once with conditional generator expressions to reduce memory footprint in your data processing code