A dictionary is another data type in Python.
A dictionary in Python is a collection of key-value pairs.
Each key is unique, and it maps to a value.
Think of a dictionary like a real-life dictionary, where you look up a word (key) to find its definition (value).
You create a dictionary like this:
student_scores = { "Alice": 85, "Bob": 92, "Charlie": 78 }
The spacing does not matter inside of a dictionary, so you could also type it like this, and this would be fine:
student_scores = { "Alice": 85,
"Bob": 92,
"Charlie": 78 }
In this dictionary, the keys are the students' names and the values are their grades.
Accessing Values
What we can now do with this is look up someone's grade by their name. Or in more general terms, we can use a key to get its value. We use square brackets for this. For example:
x = student_scores["Alice"]
print(x)
This will print out 85, since that is the value for "Alice".
Updating values
If we want to change a value in a dictionary - say Alice's grade has changed to 90 - we can do:
student_scores["Alice"] = 90
Adding new key-value pairs
To add a new item to the dictionary, just assign the key-value pair. Say, for example, that Dana is a new student that we want to add. We would do:
student_scores["Dana"] = 82
Accessing key that are not in the dictionary
If you try to access a key that is not in the dictionary, you will get an error. For example, this will cause an error:
print(student_scores["Emily"])
There are times when we may want to check whether a particular key is in the dictionary or not. (Ex: Is Fred in my dictionary already?) We can't check by just trying to access it because then our code will error out if it is not there. But we can use in to check if a key is in a dictionary:
if "Emily" in student_scores:
print(student_scores["Emily"])
else:
print("Emily not found.")
Task 1.1:
Given the following dictionary (copy/paste it into your code):
student_scores = { "Alice": 85, "Bob": 92, "Charlie": 78, "Dana": 82, "Emily": 95 }
And given a list of names, for example:
["Alice", "Brian", "Charlie", "David", "Emily", "Fred"]
Write code that will loop over the list of names and for each name, if they are in the dictionary and have a grade, print out their name and grade. But if they are not in the dictionary, then add them to the dictionary with a grade of 0 and print out that they've been added.
For example, the output would look like this:
Alice 85
Brian added
Charlie 78
David added
Emily 95
Fred added
And if you print(student_scores) at the end, it should include the added students with grades of 0:
student_scores = { "Alice": 85, "Bob": 92, "Charlie": 78, "Dana": 82, "Emily": 95, "Brian": 0, "David": 0, "Fred": 0}
Note: When you print out a dictionary, the items could be in any order - dictionaries are unordered so the order in which things print out does not matter, it only matters that the item is in there somewhere.
Values can be any data type
The value part of the key-value pair can be any data type. For example, instead of keeping each student's overall average in our dictionary, maybe we want to keep a list of all their individual grades on every assignment. We can do that! It would look like this:
student_scores = { "Alice": [85, 87, 92, 96],
"Bob": [92, 91, 94, 84],
"Charlie": [78, 80, 82, 84] }
Now, when we access the dictionary with a key, the value we get back is a list.
x = student_scores["Alice"]
print(x)
This will print out [85, 87, 92, 96].
Task 1.2:
Using the student_scores dictionary just above (with the lists of grades):
Print out Bob's grade on assignment 2
Change Charlie's grade on assignment 3 to an 85