Iterating and Sorting Lists in Python for Data Science – Best Practices 2026
Iterating and sorting lists are two of the most common operations in data science — whether ranking features by importance, sorting customer records, ordering dates, or processing results. Mastering the right techniques will make your code cleaner, faster, and more Pythonic.
TL;DR — Recommended Approaches
- Use
for item in my_listfor simple iteration - Use
enumerate()when you need index + value - Use
sorted()to get a new sorted list - Use
my_list.sort()to sort in place
1. Iterating Over Lists
features = ["amount", "quantity", "profit", "region", "category"]
# Simple iteration
for feature in features:
print(feature)
# With index using enumerate (very common in data science)
for rank, feature in enumerate(features, start=1):
print(f"Rank {rank}: {feature}")
# With zip for parallel iteration
importance = [0.42, 0.31, 0.18, 0.09, 0.05]
for feature, score in zip(features, importance):
print(f"{feature:12} : {score:.4f}")
2. Sorting Lists
# 1. sorted() - returns a new sorted list (recommended)
sorted_features = sorted(features) # alphabetical
# 2. sort() - sorts in place (modifies original)
features.sort()
# Sorting with key (very useful in data science)
feature_importance = [("amount", 0.42), ("region", 0.09), ("profit", 0.31)]
sorted_by_importance = sorted(feature_importance, key=lambda x: x[1], reverse=True)
3. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv")
# Example 1: Sort features by importance
importance_dict = {"amount": 0.42, "quantity": 0.05, "profit": 0.31, "region": 0.18}
sorted_features = sorted(importance_dict.keys(),
key=lambda f: importance_dict[f],
reverse=True)
# Example 2: Iterate over sorted results
for rank, feature in enumerate(sorted_features, start=1):
print(f"#{rank} {feature:12} importance: {importance_dict[feature]:.4f}")
# Example 3: Sort DataFrame rows and iterate
top_sales = df.nlargest(10, "amount")
for idx, row in enumerate(top_sales.itertuples(), start=1):
print(f"#{idx} Customer {row.customer_id}: ${row.amount:,.2f}")
4. Best Practices in 2026
- Use
sorted()when you want to keep the original list unchanged - Use
.sort()only when you no longer need the original order - Always use
key=parameter for custom sorting (importance, dates, etc.) - Combine
enumerate()with sorting for ranked outputs - For very large lists, consider NumPy or pandas sorting when possible
Conclusion
Iterating and sorting lists are essential skills in data science. In 2026, combine enumerate() for indexed iteration, zip() for parallel processing, and sorted() with a key function for powerful ranked outputs. These patterns will make your feature selection, result ranking, and data exploration code much cleaner and more efficient.
Next steps:
- Review your current code and improve list iteration/sorting sections using
enumerate(),zip(), andsorted(key=...)