Iterating with a for Loop in Python – Best Practices for Data Science 2026
The for loop is one of the most frequently used constructs in data science. In 2026, writing clean, efficient, and Pythonic for loops is essential for processing data, training models, and building pipelines.
TL;DR — Modern for Loop Best Practices
- Use direct iteration over iterables instead of manual indexing
- Use
enumerate()when you need both index and value - Use
zip()when iterating over multiple sequences together - Avoid modifying a list while iterating over it
1. Basic and Pythonic for Loops
scores = [85, 92, 78, 95, 88]
# Good - direct iteration
for score in scores:
print(f"Score: {score}")
# Good - when you need the index
for i, score in enumerate(scores, start=1):
print(f"Rank {i}: {score}")
# Good - iterating over multiple lists together
names = ["Alice", "Bob", "Charlie"]
for name, score in zip(names, scores):
print(f"{name} scored {score}")
2. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv")
# Iterating over columns
for column in df.columns:
print(f"Processing column: {column} (dtype: {df[column].dtype})")
# Iterating over rows safely (for small datasets)
for idx, row in df.iterrows():
if row["amount"] > 1000:
print(f"High value order from {row['region']}")
# Better approach for most cases - vectorized or itertuples()
for row in df.itertuples():
if row.amount > 1000:
print(f"High value: {row.customer_id}")
3. Common Pitfalls to Avoid
# ❌ Bad: Modifying a list while iterating over it
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
numbers.remove(num) # This can cause skipping elements!
# ✅ Better: Create a new list
numbers = [1, 2, 3, 4, 5]
even_numbers = [num for num in numbers if num % 2 == 0]
4. Best Practices in 2026
- Prefer iterating directly over the data (`for item in data`) instead of using range + indexing
- Use
enumerate()when you need the index - Use
zip()when iterating over multiple sequences in parallel - For large DataFrames, prefer vectorized operations or
itertuples()overiterrows() - Never modify the collection you are iterating over — create a new one instead
Conclusion
The for loop is a fundamental tool in data science. In 2026, writing Pythonic for loops means favoring direct iteration, using enumerate() and zip() when needed, and avoiding manual indexing. For performance-critical code with large DataFrames, prefer vectorized Pandas operations over explicit loops whenever possible.
Next steps:
- Review your current
forloops and refactor them to use more Pythonic patterns withenumerate(),zip(), and direct iteration