@extends('template') @section('title') Lab 10, Part 3: Inverting a dictionary @stop @section('content') # Lab 10: Inverting a dictionary ## Building a reverse dictionary
Partner B
Remember the [cs111dict](/labs/lab10/simple-dicts#cs111dict) (click to go to part) dictionary we built earlier in today's lab? Wouldn't it be cool to be able to create a reverse dictionary where we use the values as the keys? We will return a dictionary where the keys are what used to be the values, and each key has a list of associated values (original keys). Open the file `invert.py`. At the top of the `invert.py` file, there is a function called `makeCS111Dict` that creates the name:year cs111 dictionary, just like the dictionary you created at the start of today's lab. Since the given cs111dict dictionary has student names as keys and graduation years as values. The reverse dictionary of cs111dict will have the graduation years as keys and the students' names associated with those graduation years as values. For example, Let's take a look at the original `cs111dict` name:year dictionary. Recall that it contains 101 students. ```py >>> cs111dict.values() dict_values(['2024', '2023', '2024', '2024', '2023', '2024', '2023', '2024', '2024', '2023', '2023', '2024', '2024', '2023', '2024', '2021', '2024', '2024', '2023', '2023', '2024', '2024', '2024', '2023', '2023', '2023', '2022', '2024', '2023', '2023', '2024', '2024', '2024', '2024', '2024', '2024', '2023', '2024', '2024', '2023', '2023', '2021', '2024', '2024', '2021', '2021', '2021', '2024', '2024', '2024', '2024', '2024', '2022', '2023', '2024', '2023', '2024', '2024', '2024', '2024', '2022', '2024', '2023', '2024', '2023', '2023', '2024', '2024', '2021', '2022', '2024', '2023', '2024', '2024', '2023', '2024', '2023', '2023', '2023', '2024', '2024', '2024', '2024', '2021', '2022', '2023', '2023', '2024', '2024', '2024', '2023', '2021', '2022', '2024', '2024', '2021', '2023', '2024', '2023', '2023', '2024']) ``` Now, if we invert the original **cs111dict** dictionary, we get the **inverted** dictionary. This block of code is commented out in your `invert.py` file in your `lab08` folder (you can umcomment them once you have written your `invertDict` function). ```py 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']) ``` produces this: ```py Invert the 111 dict dict_keys(['2024', '2023', '2021', '2022']) **2021** ['Lena', 'Andrea', 'Amal', 'Ronghao', 'Alexandra', 'Rebecca', 'Jocelyn', 'Bryant', 'Claire'] **2022** ['Rumi', 'Helen', 'Lisa', 'Yujue', 'Maya', 'Ting'] **2023** ['Sarah', 'Jiyoung', 'Parul', 'Briana', 'Liliana', 'Dakota', 'Cecilia', 'Sophie', 'Dina', 'Emilia', 'Erica', 'Ananya', 'Julia', 'Grace', 'Chelsea', 'Juliette', 'Dominique', 'Molly', 'Chae', 'Marissa', 'Daisy', 'Aisha', 'Kealani', 'Preeda', 'Sapphire', 'Mira', 'Sadie', 'Havannah', 'Alyssa', 'Amelia', 'Emma', 'Lily'] **2024** ['Karina', 'Jamie', 'Angela', 'Lauren', 'Elizabeth', 'Edith', 'Calla', 'Eunji', 'Annabel', 'Maura', 'Charlotte', 'Lea', 'Victoria', 'Meha', 'Keiko', 'Diana', 'Anah', 'Youngja', 'Shan', 'Joy', 'Makiko', 'Sabina', 'Paige', 'Kalau', 'Isabella', 'HeeJoo', 'Breana', 'Ana', 'Jingyuan', 'Nicole', 'Hope', 'Susanna', 'Daneen', 'Isabelle', 'Hongyu', 'Fiona', 'Maggie', 'Amanda', 'Jennifer', 'Hannah', 'Aine', 'Trisha', 'Rune', 'Yuwei', 'Seana', 'Alice', 'Sophia', 'Kristi', 'Kathryn', 'Zoe', 'Lola', 'Maddy', 'Aimee', 'Savannah'] ``` @include('/labs/lab10/_toc') @stop