Dictionaries
A list finds things by position. A dictionary finds things by a key you choose. It is the right structure whenever you want to look something up: the price of an item, the stock for a code, the count for a word.
prices = {"apple": 40, "bread": 25, "milk": 2.5}
print(prices["bread"])
print(len(prices))
Curly braces, and each entry is key: value. prices["bread"] looks up the
value stored under the key "bread". Keys are usually strings or numbers;
values can be anything.
Adding and changing
Assigning to a key adds the entry if it is new and replaces it if it exists.
prices = {"apple": 40}
prices["bread"] = 25
prices["apple"] = 45
print(prices)
Missing keys
Looking up a key that is not there is a KeyError. Check first with in, or
use get, which returns a default instead of failing.
prices = {"apple": 40, "bread": 25}
print("milk" in prices)
print(prices.get("milk"))
print(prices.get("milk", 0))
in checks the keys, not the values: 40 in prices is False here. get
with no default returns None. get(key, 0) returns 0 when the key is
missing, which is exactly what counting needs.
Removing
prices = {"apple": 40, "bread": 25}
del prices["apple"]
print(prices)
Looping over a dictionary
Looping over a dict gives its keys. items() gives key and value pairs
together, which is the form you almost always want.
prices = {"apple": 40, "bread": 25, "milk": 2.5}
for item in prices:
print(item)
for item, price in prices.items():
print(f"{item} costs {price}")
Each round of the second loop hands over one (key, value) pair, and
writing two names after for splits the pair into them. The next page
covers this splitting, called unpacking, in full. keys() and values()
give just one side. Entries come out in the order they were added.
Counting things
The classic dictionary pattern. For each thing, look up its count with a default of 0, add one, store it back.
basket = ["apple", "bread", "apple", "milk", "apple"]
counts = {}
for item in basket:
counts[item] = counts.get(item, 0) + 1
print(counts)
Try it
Price lookup
Write price_of(prices, item) that returns the price stored for item in
the dictionary prices, or 0 if the item is not listed.
price_of({"apple": 40, "bread": 25}, "bread") -> 25
price_of({"apple": 40, "bread": 25}, "milk") -> 0
def price_of(prices, item):
...
Apply a price change
Write set_price(prices, item, price) that stores price under item in
the dictionary you are given, adding the item if it is new or replacing the
old price, and returns that same dictionary. Do not build a new one.
set_price({"apple": 40}, "bread", 25) -> {"apple": 40, "bread": 25}
set_price({"apple": 40}, "apple", 45) -> {"apple": 45}
def set_price(prices, item, price):
...
Count the items
Write count_items(basket) that returns a dictionary mapping each item in
the list basket to how many times it appears.
count_items(["apple", "bread", "apple"]) -> {"apple": 2, "bread": 1}
count_items([]) -> {}
def count_items(basket):
counts = {}
for item in basket:
...
return counts
Next: Tuples and sets.