''' Author: Sohie Lee Purpose: Lab 09 Dictionaries Date: Spring 22 Purpose: practice inverting the keys and values of a given dictionary Filename: invert.py ''' # --------------------------- # Provided code; do not edit # --------------------------- from nameYear import * def makeCS111Dict(students): '''Returns a name:year dictionary built from the nameYear.py file ''' newdict = {} for person in students: # person looks like this: ['Kathryn', '2021'] name = person[0] year = person[1] newdict[name] = year return newdict # ------------------------------------------------------------ # Write your invertDict function here # ------------------------------------------------------------ def invertDict(origDict): '''Given origDict, return an inverted dictionary such that the keys become the values and the values become the keys. ''' inverted = {} for student in origDict: year = origDict[student] if year in inverted: # year already exists as key in new dict inverted[year].append(student) else: # create new key:value pair inverted[year] = [student] # make sure it is a list return inverted #--------------# # Testing code # #--------------# cs111dict = makeCS111Dict(nameYear) print(len(cs111dict)) print("Invert the 111 dict") inverted = invertDict(cs111dict) print(inverted.keys()) print("**2021**\n",inverted['2021']) print("**2022**\n",inverted['2022']) print("**2023**\n",inverted['2023']) print("**2024**\n",inverted['2024'])