Conditionals in List Comprehensions – Best Practices for Data Science 2026
Adding conditionals (if statements) inside list comprehensions is one of the most powerful and frequently used patterns in data science. It allows you to filter and transform data in a single, clean, and efficient line of code.
TL;DR — Two Types of Conditionals
- Filtering:
[expr for item in iterable if condition] - Conditional Expression:
[expr_if_true if condition else expr_if_false for item in iterable]
1. Filtering with if (Most Common)
scores = [85, 92, 78, 95, 88, 76, 91, 65]
# Keep only high scores
high_scores = [score for score in scores if score >= 90]
# Multiple conditions
valid_high = [score for score in scores if score >= 90 and score <= 100]
print(high_scores)
2. Conditional Expressions (if-else inside)
scores = [85, 92, 78, 95, 65]
# Categorize scores
categories = ["High" if s >= 90 else "Medium" if s >= 75 else "Low" for s in scores]
print(categories)
3. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv")
# Example 1: Create new column with conditional logic
df["sales_category"] = [
"High Value" if amount > 1500
else "Medium" if amount > 800
else "Low"
for amount in df["amount"]
]
# Example 2: Filter and clean in one step
clean_amounts = [
round(amount, 2)
for amount in df["amount"]
if pd.notna(amount) and amount > 0
]
# Example 3: Feature engineering with conditions
features = [
f"{col}_log" if "amount" in col or "profit" in col else col
for col in df.columns
]
4. Best Practices in 2026
- Use simple
ifat the end for filtering - Use
if-elseinside the expression for value transformation - Keep comprehensions readable — avoid more than one nested conditional
- For very complex logic, switch to a traditional
forloop - Combine with
enumerate()orzip()when needed
Conclusion
Conditionals inside list comprehensions make data cleaning, filtering, and feature engineering extremely concise and Pythonic. In 2026, mastering the difference between filtering (if at the end) and conditional expressions (if-else inside) is essential for writing clean, efficient data science code. Use them liberally for simple cases, but fall back to explicit loops when the logic becomes too complex.
Next steps:
- Review your data cleaning and feature engineering code — replace multiple
forloops +append()with conditional list comprehensions where appropriate