List Comprehension with range() in Python – Best Practices for Data Science 2026
Combining range() with list comprehensions is a very common and powerful pattern in data science. It allows you to generate sequences of numbers, create index-based operations, or build test datasets quickly and cleanly.
TL;DR — Common Patterns
[expression for i in range(n)]– Generate sequences[f(i) for i in range(start, stop, step)]– With custom range- Very useful for creating dummy data, indices, or repeating patterns
1. Basic Usage
# Simple sequence
squares = [i ** 2 for i in range(10)]
print(squares)
# With start, stop, and step
even_numbers = [i for i in range(0, 21, 2)]
print(even_numbers)
2. Real-World Data Science Examples
import pandas as pd
import numpy as np
# Example 1: Create a list of feature names with indices
feature_names = [f"feature_{i:03d}" for i in range(50)]
# Example 2: Generate dummy data for testing
dummy_data = [
{"id": i, "value": round(np.random.normal(100, 15), 2), "category": "A" if i % 3 == 0 else "B"}
for i in range(1000)
]
df = pd.DataFrame(dummy_data)
# Example 3: Create lagged feature indices
lags = [f"lag_{i}" for i in range(1, 8)] # lag_1 to lag_7
print(lags)
3. Advanced Pattern – Nested range with Conditions
# Create a grid of combinations
grid = [(x, y) for x in range(5) for y in range(5) if x != y]
# Generate month-year strings for time series analysis
months = [f"{year}-{month:02d}" for year in range(2023, 2027) for month in range(1, 13)]
print(months[:10])
4. Best Practices in 2026
- Use
range()inside list comprehensions when you need numeric sequences or indices - Keep comprehensions readable — avoid deeply nested range expressions
- Prefer
np.arange()orpd.date_range()for large numeric or date sequences in data science - Use descriptive variable names (e.g.,
ifor index,idxfor position) - Combine with
enumerate()when you need both the generated value and its position
Conclusion
List comprehensions combined with range() provide a clean and efficient way to generate sequences and perform index-based operations in data science. In 2026, this pattern is commonly used for creating dummy datasets, generating feature names, building time series indices, and many other repetitive tasks. Use it when the logic is simple and clear.
Next steps:
- Practice using list comprehensions with
range()to generate indices, dummy data, or feature lists in your next project