Creating and Looping Through Dictionaries in Python – Comprehensive Guide for Data Science 2026
Dictionaries are one of the most essential data structures in Python data science. They store data as key-value pairs, enabling fast lookups, flexible configurations, feature mapping, model parameters, and summary statistics. Mastering how to create and loop through dictionaries will make your code cleaner, faster, and more professional.
TL;DR — Key Techniques
- Create with literal
{},dict(),zip(), or dict comprehensions - Loop with
.items()for key-value pairs (most common) - Use
.keys()or.values()when you need only one side - Modern Python guarantees insertion order
1. Creating Dictionaries
# 1. Literal syntax
feature_importance = {
"amount": 0.42,
"quantity": 0.31,
"profit": 0.18,
"region": 0.09
}
# 2. From two lists using zip (very common)
features = ["amount", "quantity", "profit"]
scores = [0.42, 0.31, 0.18]
importance = dict(zip(features, scores))
# 3. Dict comprehension
squared_importance = {k: v ** 2 for k, v in feature_importance.items()}
2. Looping Through Dictionaries
# Most common and recommended
for feature, score in feature_importance.items():
print(f"{feature:12} : {score:.4f}")
# Only keys
for feature in feature_importance.keys():
print(feature)
# Only values
for score in feature_importance.values():
print(score)
# With enumerate for ranked output
for rank, (feature, score) in enumerate(feature_importance.items(), start=1):
print(f"#{rank:2d} {feature:12} : {score:.4f}")
3. Real-World Data Science Examples
import pandas as pd
df = pd.read_csv("sales_data.csv")
# Example 1: Column mapping dictionary
col_mapping = {col: col.lower().replace(" ", "_") for col in df.columns}
# Example 2: Model configuration
model_config = {
"model_type": "random_forest",
"n_estimators": 200,
"max_depth": 10,
"random_state": 42
}
for key, value in model_config.items():
print(f"{key:15} = {value}")
# Example 3: Summary statistics per group
summary = {}
for region, group in df.groupby("region"):
summary[region] = {
"total_sales": group["amount"].sum(),
"avg_amount": round(group["amount"].mean(), 2),
"count": len(group)
}
for region, stats in summary.items():
print(f"{region}: {stats}")
4. Best Practices in 2026
- Use dict comprehensions for clean creation from iterables
- Always loop with
.items()when you need both key and value - Use
.get(key, default)for safe access - Keep configuration and hyperparameters as dictionaries
- Prefer
collections.defaultdictwhen you need automatic defaults
Conclusion
Creating and looping through dictionaries is a fundamental skill every data scientist uses daily. In 2026, dictionaries power feature mapping, model configuration, summary statistics, and clean data processing pipelines. Mastering literal creation, zip(), dict comprehensions, and .items() looping will make your code significantly more Pythonic, readable, and efficient.
Next steps:
- Review your current code and replace manual key-value handling with clean dictionary creation and
.items()loops