1
Well I would change the concept over to a course dictionary. Then for the value of each item it would have another dictionary, one key being the list of students and the other key being the list of assistants.
So your course dictionary would look like this:
{
"course_id_1": {
"students": [],
"assistants": [],
},
"course_id_2": {
"students": [],
"assistants": [],
}
}
Assuming you add the assistants the same way you do students, you could do as follows:
for (fname, lname, course) in student_enrolls:
courses[course]['students'].append(fname + ' ' + lname)
for (fname, lname, course) in assistants:
courses[course]['assistants'].append(fname + ' ' + lname)
for k in courses.keys():
courses[k]['students'].sort()
courses[k]['assistants'].sort()
That being said you could probably improve the performance of your solution by ordering your enrollment objects and then removing the .sort()
calls
Source:stackexchange.com