Lab 10, Part 1: Simple dictionaries
Cheat sheet: lists vs. dictionaries
This table shows key operations for lists and dictionaries:
Operation | For list L |
For dictionary D |
Notes |
---|---|---|---|
check # of items | len(L) |
len(D) |
Dictionaries have exactly one value per key. |
add an item | L.append(item) , or L.insert(index, item) |
D[key] = value |
Dictionary requires both a key and a value. |
replace an item | L[index] = value |
D[key] = value |
Same syntax as creating a new key for a dictionary. |
remove an item | L.pop(index) |
D.pop(key) |
Pop without argument works for lists (removes last item) but not dicts. |
check presence of an item | item in L |
key in D |
To check for a value in a dictionary, use value in D.values() |
retrieve an item | x = L[index] |
x = D[key] , or x = D.get(key) |
.get will return None if the key is missing instead of causing a KeyError . |
index loop |
|
|
Order of a dictionary is based on the order in which keys were added. |
value loop |
|
|
There's no good way to retrieve the key based on a dictionary value. If you need the key, use an index loop instead. |
In your lab10
folder there is a file called nameYear.py
that contains a list of 107 fictitious CS111 students and their corresponding graduation year.
Here is a snapshot of what the list looks like (you can open the file in Thonny). Note that it is a list of lists.
# nameYear.py
nameYear = [
...
['Sarah', '2024'],
['Preedah', '2023'],
...
['Yujue', '2022'],
['Shan', '2024'],
]
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:
from nameYear import *
Then, do the following:
- Create an empty dictionary called
cs111dict
. - For each list (student) in
nameYear
, create a dictionary entry in thecs111dict
where thekey
is the student's name and thevalue
is their year.
Check the results of your work by invoking len(cs111dict)
— there should be 101 entries.
Wait a second. Why are there only 101 entries? There are 107 students in our fictitious nameYear.py
list! 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['Annabel']
cs111dict['BugsBunny']
cs111dict['2022']
'Annabel' 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:
['Andy', '2025'], ['Sohie', '2024'], ['Peter', '2025']
At this point, our dictionary is empty:
yearDict = { }
Let's take Andy first. His year is 2025. After we add Andy to the dictionary, it looks like this:
yearDict = {'2025': ['Andy'] }
Now we add in Sohie, who has a different year, so that means a new key:value pair.
yearDict = {'2025': ['Andy'],
'2024': ['Sohie']
}
And now we add in Peter. Peter will graduate in 2025, so we can add their name to the list associated with the key '2025':
yearDict = {'2025': ['Andy', 'Peter'],
'2024': ['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:
# Create the year dictionary
yearDict = makeYearDict(nameYear)
list(yearDict.keys())
# ['2024', '2022', '2023', '2025'] # remember order doesn't matter
yearDict['2022']
# ['Angela', 'Rumi', 'Helen', 'Lisa', 'Yujue', 'Maya', 'Ting']
yearDict['2025']
# ['Lena', 'Andrea', 'Amal', 'Ronghao', 'Alexandra', 'Rebecca', 'Rebecca', 'Jocelyn', 'Bryant', 'Claire']
len(yearDict['2024'])
# 57
len(yearDict['2023'])
# 33
Table of Contents
- Lab 10 Home
- Cheat Sheet: Lists & Dictionaries
- Part 1: Exercises
- Part 2: Ironman data
- Knowledge Check