This Is The Python Code To Reverse A Dictionary and Create The Inverted Dictionary
This Is The Python Code To Reverse A Dictionary and Create The Inverted Dictionary
Original dictionary #
{ = students_courses
}{ = inverted_dict
inverted_dict[course].append(student)
:else
inverted_dict[course] = [student]
Print the original dictionary #
print("Original Dictionary:")
print(students_courses)
print(inverted_dict)
:Explanation
We define the dictionary students_courses, which contains lists of students and their
.courses
.Then, we create an empty dictionary called inverted_dict to store the inverted dictionary
We iterate through each item in students_courses and examine each course in the student's
.Finally, we print both the original dictionary and the inverted dictionary
:The output
:Original Dictionary
:Inverted Dictionary
CS1101': ['Stud1'], 'CS2402': ['Stud1', 'Stud2'], 'CS2001': ['Stud1', 'Stud2'], 'CS1102': '{
}['Stud2']