List Comprehensions in Python

Data Science Tool Box Mar 18, 2026

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 for loops with append()
  • 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 for loop 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

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices