We can remove items from dictionaries using the del operator.
grades = {"PHY": "A+", "CIV": "A", "CSC": "A+", "ESC":"B+"}
Suppose we want to remove the entry with the key "PHY". This is done similarly to removing elements from lists by specifying elements at what index we want to remove:
del grades["PHY"]
grades
We can also remove all the entries at ones:
grades.clear()
grades
Let's restore the grades to what they were, and try to remove some of the entries according to their values (note that there's no easy way to do this -- del only removes entries using their keys.
grades = {"PHY": "A+", "CIV": "A", "CSC": "A+", "ESC":"B+"}
def correct_transcript_bad(grades):
for course in grades:
#grades[course] is a grade, like "B+" or "A". We
#are checking whether grades[course] is "A" ор "А+"
if grades[course] not in ["A", "A+"]:
del grades[course]
correct_transcript_bad(grades)
This results in an error. The reason we get an error is that, just like with lists, we cannot iterate over dictionaries while modifying their contents.
Here is how to avoid the error: create a list of all the keys in the dictionary (that will from there on be independent of the dictionary), and go over all the keys, deleting the entries corresponding to them one-by-one.
def remove_bad_grades(grades):
#The following is fine, since list(grades.keys()), once created
#is not updated and is independent of the dictionary
for subj in list(grades.keys()):
if grades[subj] not in ["A", "A+"]:
del grades[subj]
The function above is equivalent to doing something like:
if grades["CIV"] not in ["A", "A+"]:
del grades["CIV"]
if grades["CSC"] not in ["A", "A+"]:
del grades["CSC"]
#...
which is completely fine.
Note that we can modify the contents of a dictionary. That means that what we've been doing above makes sense: you can write a function that modifies the contents of a dictionary, and it would make sense to call it in order to modify a dictionary
def make_CSC_100(grades):
grades["CSC"] = 100 #has an effect outside the function,
#since grades is an alias of a dictionary
#that exists outside the function
The function below still doesn't make sense, since it doesn't modify the contents of the dictionary, but rather makes the local variable grades refer to a new dictionary:
def drop_everything(grades):
grades = {} #doesn't have an effect outside the function
But the following function does make sense:
def drop_everything(grades):
grades.clear() #.clear() changes the contents of grades
Here is another function that makes sense:
def drop_everything(grades):
for subj in list(grades.keys()):
del grades[subj]
Finally, here's another idea. A way to get some key in the dictionary grades is to get a list of all the keys:
list(grades.keys())
We can get the first element of this list:
list(grades.keys())[0]
We could, if we wanted, delete the entry that corresponds to this key:
del grades[list(grades.keys())[0]]
grades
Here is the final version of drop_everything() that uses this idea:
def drop_everything2(grades):
while len(grades) > 0:
del grades[list(grades.keys())[0]]