Practical Use Cases
Data Aggregation and Counting
Word Frequency Analysis
from collections import defaultdict
def count_word_frequencies(text):
word_freq = defaultdict(int)
for word in text.split():
word_freq[word] += 1
return word_freq
text = "python python programming coding python"
result = count_word_frequencies(text)
print(dict(result))
Grouping Data
def group_students_by_grade(students):
grade_groups = defaultdict(list)
for name, grade in students:
grade_groups[grade].append(name)
return grade_groups
students = [
('Alice', 'A'),
('Bob', 'B'),
('Charlie', 'A'),
('David', 'C')
]
grouped_students = group_students_by_grade(students)
print(dict(grouped_students))
Graph and Network Processing
Adjacency List Representation
def create_graph_adjacency_list():
graph = defaultdict(list)
graph['A'].append('B')
graph['A'].append('C')
graph['B'].append('D')
return graph
network = create_graph_adjacency_list()
print(dict(network))
Caching and Memoization
Recursive Fibonacci with Memoization
def fibonacci_memoized():
cache = defaultdict(int)
def fib(n):
if n < 2:
return n
if n not in cache:
cache[n] = fib(n-1) + fib(n-2)
return cache[n]
return fib
fibonacci = fibonacci_memoized()
print(fibonacci(10))
Nested Dictionaries
def transform_data(raw_data):
transformed = defaultdict(lambda: defaultdict(list))
for item in raw_data:
category, subcategory = item.split('.')
transformed[category][subcategory].append(item)
return transformed
data = ['tech.python', 'tech.java', 'science.biology', 'tech.python']
result = transform_data(data)
print(dict(result))
Multi-dimensional Metrics
def track_performance_metrics():
metrics = defaultdict(lambda: {
'total': 0,
'count': 0,
'average': 0
})
def update_metric(category, value):
metrics[category]['total'] += value
metrics[category]['count'] += 1
metrics[category]['average'] = metrics[category]['total'] / metrics[category]['count']
return metrics, update_metric
performance, update = track_performance_metrics()
update('sales', 100)
update('sales', 200)
print(dict(performance))
Workflow Visualization
graph TD
A[Raw Data] --> B{DefaultDict Processing}
B --> C[Data Transformation]
C --> D[Grouped/Aggregated Result]
Use Case Comparison
Use Case |
Standard Dict |
DefaultDict |
Counting |
Manual Initialization |
Automatic Counting |
Grouping |
Requires Checks |
Seamless Grouping |
Caching |
Complex Implementation |
Simple Memoization |
LabEx Recommendation
At LabEx, we emphasize that defaultdict is a powerful tool for simplifying data manipulation and reducing boilerplate code in Python.
Key Takeaways
- Automatic value generation
- Simplified data processing
- Reduced error-prone code
- Enhanced readability