Professional Documents
Culture Documents
UoPeople CS 1101 Assignment Unit 7
UoPeople CS 1101 Assignment Unit 7
def invert_courses(student_course):
inverted = {}
if course in inverted:
inverted[course].append(student)
else:
inverted[course] = [student]
return inverted
# Example :
student_course = {
inverted_courses=invert_courses(student_course)
print(inverted_courses)
The code takes an input dictionary where students are keys, and their courses are
values. In this example, there are three students (Stud1, Stud2, and Stud3) and the
courses they are enrolled in. The output of the code, when using the provided example,
would be:
'CS2203': ['Stud2'],
'CS2204': ['Stud3']
This inverted dictionary now satisfies the tneed, with courses as keys and lists of
students as values, as instructed. It allows to easily see which students are enrolled in
each course.
References
1. Downey, A. (2015). Think Python: How to think like a computer scientist (2nd
2. kjdElectronics. (2017, August 5). Python beginner tutorial 8 - For loop, lists, and