Question
In a program, a dictionary contains lists of students and their courses. The teacher is interested to have a dictionary that has the courses as
In a program, a dictionary contains lists of students and their courses. The teacher is interested to have a dictionary that has the courses as key and the students enrolled in each course as values. Each key has three different values.
To address this requirement, create a function to invert the dictionary and implement a solution that satisfies the teacher's need. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary.
Sample input:
{
'Stud1: ['CS1101', 'CS2402', 'CS2001'],
'Stud2: ['CS2402','CS2001','CS1102']
}
Inverted Output:
{
'CS1101': ['Stud1'],
'CS2402':['Stud1','Stud2'],
'CS2001': ['Stud1','Stud2']
'CS 1102'['Stud2']
}
Programming Instructions:
- Print the original dictionary as well as the inverted dictionary.
- Include your the Python program and the output in your submission.
- The code and its output must be explained technically. The explanation can be provided before or after the code, or in the form of comments within the code.
ave Traceback (most recent call last): Python 3.4.3 Shell File Edit Shell Debug Options Window Help Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license ()" for more information. >>> student_courses = {'Lyllah': ['Science', 'Math', 'English'], 'Tyler': ['Science', 'History', 'Music'], 'Lillyan':['Music', 'History', 'PE'], 'Sarah': ['Spanish', 'PE', 'Science'], 'Johnathan': www 'Math', 'PE', 'English'], 'Jordan': ['History', 'Math', 'Music']} >>> print (student_courses) {"Lyllah': ['Science', 'Math', 'English'], 'Jordan': ['History', 'Math', 'Music'], 'Lillyan': ['Music', 'History', 'PE'], 'Tyler': ['Science', 'History', 'Music'], 'Sarah': ['Spanish', 'PE', 'Scie nce'], 'Johnathan': ['Math', 'PE', 'English']} navien. Tava >>> inverse_courses = invert_dict (student_courses) File " ", line 1, in Ter inverse courses = invert dict (student courses) NameError: name 'invert_dict' is not defined >>> def invert dict (student courses) : OCLOCO for key in student_courses: val student courses [key] if val not in inverse: SyntaxError: invalid syntax >>> def invert dict (student courses): for key in student courses: >>> H inverse [val] = [key] else: val student_courses [key] if val not in inverse: else: return inverse >>> invert_dict >>> print (invert_dict) inverse [val] = [key] inverse [val].append (key) >>> print (invert_dict (student_courses)) Traceback (most recent call last): File " ", line 1, in print (invert_dict (student_courses)) File " ", line 4, in invert_dict if val not in inverse: NameError: name 'inverse' is not defined Type here to search (99+ 81F Sunny T ^ 0 4>) X Ln: 3 Col: 6 5:46 PM 10/23/2023
Step by Step Solution
3.52 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
Solutions Step 1 Inverting the dictionary Explanation The teacher has a studentcourse dictionary where students are keys and their enrolled courses are lists as values They require an inverted diction...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started