Calculate Class Averages
Advanced · Write the code · from Working with Nested Lists, Dicts, and Complex Data Structures
From the lesson: Working with Nested Lists, Dicts, and Complex Data Structures
Think about a school. A school has classrooms. Each classroom has students. Each student has a name, an age, and a list of grades. That's data inside data inside data - nested data.
Read the lessonYou have a dictionary where each key is a class name and each value is a list of student dictionaries. Calculate the average grade for each class and print it in the format: ClassName: average
Round each average to 1 decimal place. Print classes in alphabetical order.
classes = {
'Math': [
{'name': 'Alice', 'grade': 90},
{'name': 'Bob', 'grade': 80},
],
'English': [
{'name': 'Charlie', 'grade': 85},
{'name': 'Diana', 'grade': 95},
{'name': 'Eve', 'grade': 78},
],
}
# Calculate and print each class average
More nested data exercises
- Access Nested List ItemsBeginner
- Predict the Output: Nested Dict AccessBeginner
- Find the Highest ScorerIntermediate
- Group Words by First LetterIntermediate
- Fix the Bug: Nested List TrapIntermediate
All Python exercises · 50 practice problems · Data Structures quiz