Editor

Type a Data Processing Function

Advanced · Write the code · from Python Type Hints: Add Types, Generics, and the typing Module

From the lesson: What Is the Difference Between Runtime and Static Type Checking?

This is the most important thing to understand about Python type hints: they exist in two separate worlds.

Read this part of the lesson

Write a fully type-annotated function summarize_scores that takes a Dict[str, List[int]] mapping student names to their test scores, and returns a Dict[str, float] mapping each student to their average score.

scores = {
    'Alice': [90, 85, 92],
    'Bob': [78, 88, 84]
}
print(summarize_scores(scores))
# {'Alice': 89.0, 'Bob': 83.33}

Round averages to 2 decimal places.

from typing import Dict, List
def summarize_scores(scores):
# Add type hints and implement
pass
data = {
'Alice': [90, 85, 92],
'Bob': [78, 88, 84]
}
result = summarize_scores(data)
for name in sorted(result):
print(f'{name}: {result[name]}')

More Type Hints exercises

All Python exercises · 50 practice problems · Advanced Python quiz