The request

From Debra Marshall, Warehouse Manager
Your goal
Update stock so Brass Floor Lamp goes up by 20, Oak Side Table goes down by 5, and a new key 'Stoneware Mug Set' holds 48. Set result to the updated dictionary (all four products).
- Store a dict in
resultnot done yet
Your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
What Debra gets, and why
The lesson
Looking things up by name
A list finds items by position. A dictionary (dict) finds them by a key that you choose.
It’s written with curly braces and key: value pairs:
stock = {
"Oak Side Table": 14,
"Brass Floor Lamp": 0,
"Linen Throw Pillow": 37,
}
stock["Oak Side Table"] # 14
Think of it as a two-column lookup table. Keys are unique: one product, one stock count. Keys are usually strings or numbers; values can be anything, including lists or other dictionaries.
Changing and adding
Assigning to a key updates it if it exists and adds it if it doesn’t:
stock["Brass Floor Lamp"] = 20 # update
stock["Oak Side Table"] -= 5 # change relative to the current value
stock["Stoneware Mug Set"] = 48 # new key
del stock["Linen Throw Pillow"] removes a key, and "Oak Side Table" in stock checks whether a
key exists (True or False).
Missing keys
Reading a key that doesn’t exist raises a KeyError:
stock["Walnut Bookshelf"] # KeyError: 'Walnut Bookshelf'
When a key might be missing, use .get, which returns a default instead of failing:
stock.get("Walnut Bookshelf", 0) # 0
stock.get("Oak Side Table", 0) # 9 after the sale
.get is the safe way to read data you didn’t create yourself, such as an API response or a row
from a file where some fields may be absent.
Looping over a dictionary
.items() gives you each key and value together, which is the loop you’ll write most:
for product, count in stock.items():
if count == 0:
print(f"{product} is out of stock")
.keys() and .values() give just one side, so sum(stock.values()) is the total number of units
on hand.
Order and equality
Since Python 3.7, dictionaries remember the order you inserted keys, which makes printed output
predictable. Two dictionaries are equal when they hold the same keys with the same values,
regardless of order, so {"a": 1, "b": 2} == {"b": 2, "a": 1} is True.
Why analysts care
Dictionaries show up everywhere: a row of data, a JSON record from a web API, a mapping from codes
to labels ({"WEB": "web", "APP": "app"}), a set of parameters for a function. pandas uses them
too, for example to rename columns or to map values in a column.
Common mistakes
- Using square brackets to read a key that may not exist. Use
.getwith a default. - Typos in keys.
"Oak side table"is a different key from"Oak Side Table", so assigning to it quietly creates a fourth product. - Expecting
result = stockto make a copy. Both names point at the same dictionary, so a later change tostockalso shows up inresult. Usestock.copy()when you need a separate one.
Lantern lit!
Practised: dictionarieskeys valuesgetupdate
- Solved
- No hints
- First run
