Programming Assignment Unit 7
Programming Assignment Unit 7
Ayesha Gul
Dr Josiah Jolaoluwa
students and their enrolled courses. In the given dictionary, students are the keys, and their
respective course lists are the values. The goal is to transform this structure so that courses
become the keys, and the students enrolled in each course become the values. To achieve this, the
function initializes an empty dictionary called inverted_dict. It then iterates through each student
and their corresponding courses using a for loop. For each course in a student's list, the function
checks whether the course already exists as a key in inverted_dict. If not, it initializes an empty
list for that course. Then, it appends the student's name to the list corresponding to that course.
Once all students and courses are processed, the function returns the newly structured dictionary.
The program also prints both the original and inverted dictionaries to showcase the
transformation. This approach effectively groups students based on the courses they are enrolled
Input:
3
Programming Assignment Unit 7
Explanation:
The input is a dictionary named student_courses, where each key represents a student,
and the corresponding value is a list of courses they are enrolled in. In this case, three
students—Ayesha, Ashhar, and Rafay—are registered for various courses. Ayesha is taking
"CS1101," "CS2402," and "CS2001." Ashhar is enrolled in "CS2402," "CS2001," and "CS1102."
Rafay is registered for "CS1101" and "CS2001." The structure of this input allows for multiple
students to share the same course. The goal of the function is to reorganize this data by making
courses the keys and listing the students under each respective course.
Output:
4
Programming Assignment Unit 7
Explanation:
inverted dictionary where courses become the keys, and the students who are enrolled in each
course become the values. In the above output, each course has a list of students who are
enrolled in it. For example, "CS1101" is taken by Ayesha and Rafay, while "CS2001" is shared
by all three students. The function achieves this by iterating through each student and their
courses, ensuring that each course is properly mapped to its respective students in a structured
format. This transformation allows a teacher to quickly see which students are in each course,
https://docs.python.org/3/using/windows.html
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf