Nested Loops in Python – Best Practices for Data Science 2026
Nested loops (a loop inside another loop) are frequently needed in data science for tasks like comparing pairs of items, creating cross-products, processing multi-dimensional data, or iterating over groups. However, they can quickly become slow and hard to read if not handled carefully.
TL;DR — Key Guidelines
- Use nested loops only when truly necessary
- Prefer list comprehensions or
itertools.product()for simple cases - Keep the inner loop as simple as possible
- Watch out for O(n²) performance on large datasets
1. Basic Nested Loop
teams = ["A", "B", "C"]
scores = [85, 92, 78]
for team in teams:
for score in scores:
print(f"Team {team} - Score: {score}")
2. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv")
# Example 1: Compare every pair of regions (careful with large data)
regions = df["region"].unique()
for i, region1 in enumerate(regions):
for region2 in regions[i+1:]: # Avoid duplicate pairs
sales1 = df[df["region"] == region1]["amount"].sum()
sales2 = df[df["region"] == region2]["amount"].sum()
print(f"{region1} vs {region2}: ${sales1:,.0f} vs ${sales2:,.0f}")
# Example 2: Feature interaction pairs using nested loops
features = ["amount", "quantity", "profit"]
interactions = []
for i, f1 in enumerate(features):
for f2 in features[i+1:]:
interactions.append(f"{f1}_x_{f2}")
print("Interaction features:", interactions)
3. Better Alternatives to Nested Loops
from itertools import product
# Cleaner way for Cartesian product
for team, score in product(teams, scores):
print(f"Team {team} - Score: {score}")
# List comprehension version
pairs = [(team, score) for team in teams for score in scores]
4. Best Practices in 2026
- Limit nesting depth — two levels is usually the maximum for readability
- Use
itertools.product()or list comprehensions for simple Cartesian products - Avoid nested loops on large datasets; consider vectorized Pandas/NumPy operations instead
- Use early break/continue when possible to reduce unnecessary iterations
- For pair-wise comparisons, consider
scipy.spatial.distanceor correlation matrices
Conclusion
Nested loops are a fundamental tool but should be used judiciously in data science. In 2026, the best practice is to replace simple nested loops with list comprehensions or itertools.product() whenever possible, and to keep any remaining nested loops clean and shallow. For performance-critical code on large data, prefer vectorized operations over explicit nested iteration.
Next steps:
- Review any nested loops in your codebase and see if they can be simplified with comprehensions or itertools