1. Concept library
  2. Python Foundations
  3. Module 5: Dictionaries and sets

Concept

Dictionary lookups

A dictionary maps keys to values and finds a key in constant time, however many it holds. It is the right structure whenever you look things up by name or id.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain why a dictionary finds a key in constant time and what makes a valid key
  2. Write code that reads, adds and updates entries in a dict
  3. Use get() or in to handle a key that may be missing

In practice

A few lines that show Dictionary lookups doing its job.

Python
stock = {"apple": 12, "pear": 0}

print(stock["apple"])              # 12
print(stock.get("plum"))           # None, no error
print(stock.get("plum", 0))        # 0
stock["plum"] = 4
print("pear" in stock)             # True

The common mistake

Watch for

Reading a key that may be missing with square brackets. stock["plum"] raises KeyError. Use get() with a default, or check with in first.

What to know first

Your roadmap puts these before Dictionary lookups, unless you already know them.

How it stays learned

Once a session shows you understand Dictionary lookups, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Dictionary lookups.