Written by Elyse Cornwall
This week in section, we'll work with the dictionary data structure, and see how it differs from lists in Python. We'll start by practicing two key dictionary operations: accessing and updating the value associated with a certain key. Next, we'll use nested dictionaries to store more complex information.
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 nested grocery dictionary from your disorganized grocery list.
Throughout the week, you make notes of items you need, where to buy them from, and how many of each to
buy. You format each entry in your list as follows: store name:item name,count
. Unfortunately, you
lazily add each item to 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
dictionary 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 (strings) as the keys and nested dictionaries as the values.
Within each store's nested dictionary, the keys are item names (strings), and the values are the number of that
item we need to buy (ints).
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,1
safeway:flour,1
safeway:eggs,2
Now 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
dictionary like we've seen above, and adds a new item to our dictionary as specified by
the parameters store
, item
, and num
, which represent the store name, item
name, and number of that item
we need.
Hint: Let the different cases from the warmup 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.
Implement the function make_groceries(filename)
, which should 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. As a reminder,
here is the structure of a common file reading loop:
with open(filename) as f:
for line in f:
line = line.strip()
# Use line here
For a refresher on what these grocery files look like, check out short-list.txt
or
long-list.txt
in the Python project linked at the top of this handout. 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 (the name of a text file) 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