@extends('template')
@section('title')
Lab 10, Part 1: Simple dictionaries
@stop
@section('content')
# Lab 10, Part 1: Simple dictionaries
## Task 1: Create a **name:year** cs111 dictionary
In your `lab10` folder there is a file called `nameYear.py` that contains a list of current CS111 students and their corresponding graduation year.
Here is a snapshot of what the list looks like:
```py
# nameYear.py
nameYear = [
['Sophia', '2022'],
['Emily', '2021'],
...
['Rebecca', '2021'],
['Alexandra', '2023'],
]
```
Your first task is to create a Python **dictionary** from this data where the **`key` is the student names** and the **`value` is the corresponding years**.
Make a new file called `simpleDictionaries.py`. At the top of the file, import the provided `nameYear` data:
```py
from nameYear import *
```
Then, do the following:
1. Create an empty dictionary called `cs111dict`.
2. For each list (student) in `nameYear`, create a dictionary entry in the `cs111dict` where the `key` is the student's name and the `value` is their year.
Check the results of your work by invoking `len(cs111dict)`— there should be **86** entries.
Wait a second. Why are there only **86** entries? There are **93** students enrolled in CS111 this semester! What happened?
Once your cs111dict is built, try out the following statements and describe/discuss the results with your partner:
- `list(cs111dict.items())`
- `list(cs111dict.keys())`
- `list(cs111dict.values())`
- `cs111dict['Anna']`
- `cs111dict['BugsBunny']`
- `cs111dict['2022']`
- `'Anna' in cs111dict`
- `'WonderWoman' in cs111dict`
## Task 2: Create a **year:[names]** CS111 dictionary
Now, using the same `nameYear` variable, write a function called `makeYearDict` that returns a dictionary where the **keys** are the year of graduation and the **values** are lists of students who will graduate in that year.
Let's say you want to build your dictionary from scratch and we want to add these students:
```py
['Andy', '2021'], ['Sohie', '2020'], ['Cibele', '2021']
```
At this point, our dictionary is empty:
```py
yearDict = { }
```
Let's take Andy first. His year is 2021. After we add Andy to the dictionary, it looks like this:
```py
yearDict = {'2021': ['Andy'] }
```
Now we add in Sohie, who has a different year, so that means a new key:value pair.
```py
yearDict = {'2021': ['Andy'],
'2020': ['Sohie']
}
```
And now we add in Cibele. Cibele will graduate in 2021, so we can add her name to the list associated with the key '2021':
```py
yearDict = {'2021': ['Andy', 'Cibele'],
'2020': ['Sohie']
}
```
Hint: remember that each **key** in a dictionary must be unique. So if the key already exists in your dictionary, then add the name to list associated with that key.
Here is some sample testing code:
```py
# Create the year dictionary
yearDict = makeYearDict(nameYear)
list(yearDict.keys())
# ['2020', '2022', '2023', '2021'] # remember order doesn't matter
yearDict['2020']
# ['Olivia','Stephanie', 'Sandra', 'Jing', "Lydia', 'Megan']
yearDict['2021']
#['Alexandria', 'Courtney', 'Alexa ', 'Nia', 'Sara', 'Hope',
# 'Sophia', 'Carson', 'Fatima', 'Meghan']
# Test 2022 and 2023 too!
```
@include('/labs/lab10/_toc')
@stop