PythiloLesson 17 of 30 · progress kept in this browser

Dicts and sets

A dict maps keys to values. Lookup by key takes about the same time however large the dict is. Dicts keep insertion order.

ages = {"ada": 36}
ages["alan"] = 41
print(ages.get("bob", 0))  # 0, no error
print("ada" in ages)       # True
for k, v in ages.items():
    print(k, v)

d[key] raises KeyError when the key is missing. d.get(key, default) does not. Keys must be hashable, so strings, numbers and tuples work and lists do not.

Your task

Write word_counts(text) returning a dict from lower-cased word to how many times it appears. Write common_items(a, b) returning a sorted list of the values present in both lists, without duplicates.