@extends('template') @section('title') Lab 11, Part 2: Inverting a dictionary @stop @section('content') # Lab 11: Inverting a dictionary ## Building a reverse dictionary Remember the [cs111dict](/labs/lab08/simple-dicts#cs111dict) (click to go to that lab) dictionary we built during our last 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 block of code to load in a pre-existing name:year dictionary called `cs111`. Just like the dictionary you created at the start of today's lab, the given cs111 dictionary has student names as keys and graduation years as values. The reverse dictionary of cs111 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 `cs111` name:year dictionary: ```py >>> len(cs111) 101 >>> 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 **cs111** dict, we get the **inverted** dict. ```py >>> inverted = invertDict(cs111) >>> inverted.keys() dict_keys(['2024', '2023', '2021', '2022']) >>> inverted['2021'] ['Lena', 'Andrea', 'Amal', 'Ronghao', 'Alexandra', 'Rebecca', 'Jocelyn', 'Bryant', 'Claire'] >>> inverted['2022'] ['Rumi', 'Helen', 'Lisa', 'Yujue', 'Maya', 'Ting'] >>> inverted['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'] >>> inverted['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/lab11/_toc') @stop