You are on page 1of 3

UNIT 7 PROGRAMMING ASSIGNMENT

Problem Overview

The task involves taking a dictionary with students and their courses as values and converting it
into an inverted dictionary where courses are keys, and the students enrolled in each course are
the values. Each course can have multiple students.

Step-by-Step Solution Using the Sample Input:

1.Initialize an empty inverted dictionary:

Begin by creating an empty dictionary, `inverted_dict`, which will hold the inverted data. In
this dictionary, the courses will be keys, and students' names will be values.

inverted_dict = {}

2. Use the provided sample input:

Using the sample input provided:

original_dict = {

'Stud1': ['CS1101', 'CS2402', 'CS2001'],

'Stud2': ['CS2402', 'CS2001', 'CS1102'] }

3. Iterate through the original dictionary and invert it:

Loop through the original dictionary. For each student, iterate through their list of courses.
Check if each course already exists as a key in the `inverted_dict`. If it does, append the current
student to the list of values. If the course key doesn't exist, create a new key-value pair with the
course and the student.

for student, courses in original_dict.items():

for course in courses:


if course in inverted_dict:

inverted_dict[course].append(student)

else:

inverted_dict[course] = [student]

4. Print the Original and Inverted Dictionaries:

To comply with the programming instructions, print both the original and inverted dictionaries:

print("Original Dictionary:")

print(original_dict)

print("\nInverted Dictionary:")

print(inverted_dict)

Explanation

This problem requires transforming a dictionary that contains students and their courses into an
inverted dictionary. In the inverted dictionary, courses are the keys, and the values are lists of
students enrolled in each course. The code makes use of Python dictionaries as the primary data
structure.
First, an empty dictionary, `inverted_dict`, is initialized to store the inverted data. This dictionary
will be populated with course codes as keys and lists of students' names as values.

The provided sample input, `original_dict`, is used to demonstrate the solution. It contains two
students, 'Stud1' and 'Stud2', along with their respective courses.

The code then iterates through `original_dict`, using a loop to process each student and their
courses. Within this loop, a nested loop iterates through each course of the current student.

For each course, the code checks if it already exists as a key in the `inverted_dict`. If it does, it
appends the current student's name to the list of values associated with that course. If the course
key doesn't exist, a new key-value pair is created. The key is the course code, and the value is a
list containing the current student.

This solution is dynamic and can handle any number of students and courses. The output of this
code demonstrates that it successfully transforms the data into the required format, with courses
as keys and lists of students as values. This approach effectively inverts the data structure while
adhering to the teacher's specifications.

References:

• Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green Tea
Press.

• kjdElectronics. (2017, August 5). Python beginner tutorial 8 - For loop, lists, and dictionaries
[Video]. YouTube. https://youtu.be/bE6mSBNp4YU

• Raj, A. (2022, March 3). Reverse a dictionary in Python. PythonForBeginners.

• Python Dictionaries. (n.d.). W3schools.

You might also like