When collecting scores by filename and subject, use dict.setdefault to create a missing list without replacing an existing one. It returns the value for the key, then lets the next operation append to that list. section 5.27
Build a nested score list
scores[filename].setdefault(subject, []) scores[filename][subject].append(score) The first line ensures that the subject key has a list. Repeated subjects keep their existing list, so each score can be appended during iteration. section 5.27
if k not in d: d[k] = v
Compare setdefault with the explicit missing-key check it replaces: if k is not in d, assign d[k] = v. section 5.27

