You can do `team_media_by_year.get(media.year, []).append(media)` instead of checking `if media.year in team_media_by_year` here.