List Comprehensions in Python
List comprehensions are one of Python’s most elegant and powerful features. They allow you to create new lists by transforming and filtering existing iterables in a single, readable line. In data science, they are widely used for data cleaning, feature engineering, and transforming datasets.
TL;DR — Basic Structure
[expression for item in iterable if condition]- More readable than traditional
forloops withappend() - Faster and more Pythonic for simple transformations
1. Basic List Comprehensions
scores = [85, 92, 78, 95, 88, 76, 91]
# Filter and transform in one line
high_scores = [score for score in scores if score >= 90]
squared_scores = [score ** 2 for score in scores]
print(high_scores)
print(squared_scores)
2. Real-World Data Science Examples
import pandas as pd
# Create a small demo file so this example runs as-is
# (in your project, sales_data.csv is your real export)
import numpy as np
rng = np.random.default_rng(42)
n = 60
pd.DataFrame({
"order_id": range(1001, 1001 + n),
"order_date": pd.date_range("2026-01-01", periods=n, freq="3D"),
"customer_id": rng.integers(101, 121, n),
"customer_name": rng.choice([" ana", "ben ", "chen", "dana"], n),
"product_id": rng.integers(1, 9, n),
"region": rng.choice(["North", "South", "East", "West"], n),
"category": rng.choice(["Electronics", "Computers", "Books", "Home"], n),
"payment_method": rng.choice(["card", "cash", "wallet"], n),
"segment": rng.choice(["consumer", "business"], n),
"quantity": rng.integers(1, 6, n),
"amount": rng.uniform(50, 2500, n).round(2),
"discount": rng.choice([0.0, 0.05, 0.1, 0.2], n),
}).assign(
price=lambda d: (d["amount"] / d["quantity"]).round(2),
profit=lambda d: (d["amount"] * rng.uniform(0.05, 0.3, n)).round(2),
).assign(
ship_date=lambda d: d["order_date"] + pd.Timedelta(days=2),
status=lambda d: np.where(d["amount"] > 2000, "priority", "standard"),
).to_csv("sales_data.csv", index=False)
df = pd.read_csv("sales_data.csv")
# Example 1: Create new columns using list comprehension
df["log_amount"] = [0 if x <= 0 else round(x ** 0.5, 2) for x in df["amount"]]
# Example 2: Filter and extract specific data
high_value_customers = [
(row.customer_id, row.amount, row.region)
for row in df.itertuples()
if row.amount > 1500
]
# Example 3: Clean and transform text data
clean_names = [name.strip().title() for name in df["customer_name"] if isinstance(name, str)]
3. When to Use List Comprehensions vs Traditional Loops
price_strings = ["19.99", " 5 ", "", "7.50"] # demo input
# Good use of list comprehension - simple and clean
prices = [float(x) for x in price_strings if x.strip() != ""]
# Better as traditional loop when logic is complex
processed = []
for x in price_strings:
cleaned = x.strip()
if cleaned:
try:
processed.append(float(cleaned))
except ValueError:
continue
4. Best Practices in 2026
- Use list comprehensions for simple filtering and transformations
- Avoid very complex list comprehensions — they can become hard to read
- For complex logic, use a traditional
forloop with.append() - Consider generator expressions (
(...)) when working with very large data to save memory - Keep comprehensions focused on one clear transformation
Anatomy of a List Comprehension
A list comprehension has the form [expression for item in iterable if condition]. Python evaluates the expression for each item that passes the optional condition and collects the results in a new list.
It is equivalent to a loop with append, but shorter and usually faster because the loop runs inside the interpreter's optimised machinery. The condition filters items; to choose between two values, put an if-else expression in the expression part instead.
Keep comprehensions short. If it no longer fits on one or two lines, or needs error handling, a regular loop is easier to read.
Worked Example: Filter, Transform and Choose
We filter scores, transform them and label each one, all with comprehensions.
scores = [55, 92, 78, 99, 64]
passed = [s for s in scores if s >= 60]
curved = [min(s + 5, 100) for s in scores]
labels = ["pass" if s >= 60 else "fail" for s in scores]
print(passed)
print(curved)
print(labels)
pairs = [(s, s >= 90) for s in scores if s >= 60]
print(pairs)
The filter keeps four scores, the transformation caps the curve at 100, and the if-else expression labels each score without removing any. The last comprehension filters and builds tuples at the same time.
Common Mistakes
- Placing an if-else after the for clause, which is a syntax error.
- Nesting several loops and conditions in one line.
- Using comprehensions only for side effects like printing.
- Building huge lists when sum() over a generator would do.
Quick Summary
- [expr for x in it if cond] filters and transforms.
- if-else in the expression chooses values.
- Keep comprehensions short.
- Use loops for complex logic.
Conclusion
List comprehensions are a hallmark of Pythonic code in data science. In 2026, they are the preferred way to create new lists through simple filtering and transformation. Use them liberally for clean, concise code, but switch to traditional for loops when the logic becomes complex or requires multiple steps. The goal is readability and maintainability, not cleverness.
Next steps:
- Review your current code and replace simple list-building loops with list comprehensions where the logic is clear and simple