Nested Dictionary
Completed
Read n lines student subject mark. Store marks in a nested dict {student: {subject: mark}}. Then print each student (sorted) with their average mark to 1 decimal place.
Input
Line 1: n. Then n lines.
Output
student average per line.
Example
Input
4 ann math 90 ann art 71 bob math 60 bob art 66
Output
ann 80.5 bob 63.0
Editor
Python
Hints
marks.setdefault(student, {}) creates the inner dict on first use.
values() gives just the marks.
Solution explanation
Nested dicts model structured data, like JSON.
Show solution
n = int(input())
marks = {}
for _ in range(n):
student, subject, mark = input().split()
marks.setdefault(student, {})[subject] = int(mark)
for student in sorted(marks):
subjects = marks[student]
print(f"{student} {sum(subjects.values()) / len(subjects):.1f}")