Written by Elyse Cornwall
This week in section, we'll get practice with the dictionary data structure, and see how it differs from lists. We'll get and set the value associated with a certain key in the dictionary, and use nested dictionaries to represent complex information. As an intermediate step, we'll store our inner structures in variables, which makes these nested structures easier to work with.
Here's a problem you can work through in the Python interpreter to review some common dictionary operations.
Let's say we have a dictionary classes where the keys are class names (strings) and the values are
the unit count for that class (ints).
>>> classes = {'CS106A': 5, 'PSYCH1': 5, 'PWR1': 4}
Write a single line of code to:
'PSYCH1'.'CS106A', changing it from 5 to 3.'AMSTUD107' with value 4.In this problem, you'll create a helpful grocery dictionary from your disorganized grocery list.
Throughout the week, you make notes of ingredients, where to buy them from, and how many to buy. Unfortunately, you lazily jot down each item at the bottom of a long list, rather than checking if it was already in the list and keeping a tally. When it's time to go shopping, you've got a grocery list that looks like this:
safeway:eggs,1
costco:croissant,12
safeway:coconut milk,3
target:sugar cookies,1
safeway:flour,1
safeway:eggs,2
trader joes:pineapple,4
trader joes:grapes,1
costco:coffee,2
trader joes:kale,1
How do you know how many eggs you need to buy at Safeway? It's pretty tedious to scan through the entire list and tally each item in case of any duplicates. We're going to write a program that can help.
groceries Dictionary
We want to build a dictionary, which we'll call groceries that stores your items so that it's easy
to see what you need from each store. Here's what the groceries dict would look like for the
example list above:
groceries = {
'safeway': {'eggs': 3, 'coconut milk': 3, 'flour': 1},
'costco': {'croissant': 12, 'coffee': 2},
'target': {'sugar cookies': 1},
'trader joes': {'pineapple': 4, 'grapes': 1, 'kale': 1}
}
Now that our data's organized in a new way, it's easier to see that we need 3 cartons of eggs from Safeway. Our
groceries dictionary has store names as the keys and then inner dictionaries as values. Within each
store's inner dictionary, the keys are item names, and the value is the number of that item we need to buy this
week.
Let's consider a smaller groceries dictionary, shown below, that we got after reading the first
three lines of our grocery list and putting them into our dictionary. How would this dictionary look after
reading in the following lines from our file? Hint: It's a good idea to write this out!
groceries = {
'safeway': {'eggs': 1, 'coconut milk': 3},
'costco': {'croissant': 12}
}
target:sugar cookies,1safeway:flour,1safeway:eggs,2Now that you've got a feel for how to add a single item to groceries, let's implement this process
in Python. We want to write the function add_item(groceries, store, item, num) which takes a
groceries dict like we've seen above, and adds a new item to our dict as specified by the
parameters store, item, num. If you check out the starter project, we've provided some code to
help you get started.
Hint: Let the different cases from the warmup to guide your code. What do we do if we haven't seen a store name before? What if we've seen both the store and item before?
Now, you'll read your entire grocery list in from a text file and construct a groceries dictionary.
To implement the function make_groceries(filename), call your add_item() helper
function in a file reading loop to build up a groceries dictionary, then
return groceries at the end of the function after reading all the lines of the file.
For a refresher on what these grocery files look like, check out short-list.txt or
long-list.txt in the starter project. You can use .find() or .split() to
separate out the important parts of each line.
You did it - you've got a program that can turn a messy grocery list into a helpful dictionary! To run your
program from the terminal, run groceries.py with one argument (a text filename) to read in all the
data from that file and print out a summary of your groceries.
$ python3 groceries.py long-list.txt
You need 3 eggs(s) from safeway
You need 3 coconut milk(s) from safeway
You need 1 flour(s) from safeway
You need 12 croissant(s) from costco
You need 2 coffee(s) from costco
You need 1 sugar cookies(s) from target
You need 4 pineapple(s) from trader joes
You need 1 grapes(s) from trader joes
You need 1 kale(s) from trader joes