What you will learn
- Use loops to traverse through key/value pairs in a dictionary
Reading
Do Now
1. In your Console
Type the following
my_dictionary = {
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5
}
print(list(my_dictionary))
In your Notebook
Respond to the following
- Write down what
list()does when given a dictionary. - What type does
list()return? - Write down how you might use a
forloop to go through and print the values ofmy_dictionary.
2. In your Console
Challenge yourself
Try your solution to 3.
Lab
In this lab we will use our word-counting code from Lab 6.02 to create a program that determines the top 5 most commonly used words in a passage of text. After processing the passage, it prints the top 5 words and the number of times each occurs.
Here’s one strategy for completing this lab:
- Repackage some of your code from Lab 6.02 to make two functions:
text_to_word_list(), that takes a single passage of text and splits into a list of words; andcount_frequencies(), that takes in a list of words and returns a dictionary of word frequencies - Write a new function,
find_max_valued_key(), that takes a dictionary as an argument, and returns the key that is associated with the largest value in that dictionary. Internally, this function loops through the dictionary while keeping track of the largest value it’s seen so far and the key that goes along with that value. - Run
find_max_valued_key()once on the dictionary of word counts, print out the key/value of word it returns. - Remove that key from the dictionary.
- Repeat steps 3-4 four more times: Call
find_max_valued_key(), print out the key/value pair, and remove the key.
If there is a tie within find_max_valued_key(), choose among the tied
items however you like and return just one of them.
Example
Here’s an example of the program output with the text passage set to the opening lines of Dr. Seuss’s poem Green Eggs and Ham:
I am Sam. I am Sam. Sam-I-am.
That Sam-I-am! That Sam-I-am!
I do not like that Sam-I-am!
Would you like green eggs and ham?
I do not like them, Sam-I-am.
I do not like green eggs and ham.
Would you like them here or there?
I would not like them here or there.
I would not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Would you like them in a house?
Would you like them with a mouse?
I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
>>> python3 most_frequent_words.py
i, 22
like, 17
not, 13
do, 11
them, 12
Bonus
The process of finding the largest element, printing it, and removing it from the dictionary is a way to sort items. Write a function that will return a sorted list of all the words from most frequent to least frequent.
Change the code to find the least frequent words.
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. What does for k in my_dictionary: loop over?
- The keys
- The values
- The key-value pairs as one string
- The number of items
Looping over a dictionary gives you each key, and you can use the key to look up the value.
2. For d = {'x': 1, 'y': 2}, what does list(d) return?
[1, 2][('x', 1), ('y', 2)]{'x', 'y'}['x', 'y']
list() on a dictionary builds a list of its keys.
3. Which loop adds up all the values of d?
for k in d: total += kfor k in d: total = dfor k in d: total += d[k]for k in d: total += len(d)
Each key is used to look up its value with d[k], and those values are accumulated into total.