1|Page
Programming Assignment Unit 7
By
Zin Myo Tun
Department of Computer Science, University of the People
CS-1101-01: Programming Fundamentals - AY2024-T2
Supervised By
Dr Vikas Thada
Instructor, Department of Computer Science
University of the People
January 4, 2024
Zin Myo Tun, C1839791
2|Page
Programming Assignment Unit 7
A Python function that inverts a dictionary where students are associated with their respective
courses, and the output is a dictionary with courses as keys and lists of students enrolled in each
course:
The invert_dictionary function is designed to address the teacher's requirement of having a
dictionary with courses as keys and students enrolled in each course as values. The function uses
two nested loops to iterate through the original dictionary, where each key is a student, and the
corresponding value is a list of courses for that student.
For each course in the list of courses for a student, the function checks whether the course is
already a key in the inverted_dict. If the course is already a key, it appends the current student to
the list of values for that key, but with a constraint: each key can have a maximum of three
values (students). This ensures that the teacher gets exactly three students for each course.
Zin Myo Tun, C1839791
3|Page
If the course is not a key, it creates a new key with the course and sets the value as a list
containing the current student. This process continues until the inverted dictionary is fully
constructed.
The sample input provided includes two students ('Stud1' and 'Stud2') with their respective
courses. The output demonstrates the inverted dictionary, satisfying the teacher's requirement,
with courses as keys and three students enrolled in each course as values (or fewer if there are
not enough students).
Word Count – 240 words
(Allen Downey, 2015)
References
Allen Downey, J. E. (2015). How to Think Like a Computer Scientist. In A. Downey, Think Python. 9
Washburn Ave, Needham MA 02492: Green Tea Press.
kjdElectronics. (2017, August 5). Python beginner tutorial 8 - For loop, lists, and dictionaries [Video].
YouTube. https://youtu.be/bE6mSBNp4YU
Python Dictionaries. (n.d.). W3schools.
Zin Myo Tun, C1839791