Think of a dictionary as a row of labelled drawers in an office filing cabinet. You do not count how many steps to take down the hall; you walk straight to the label and open it. That is why dictionaries are fast for lookups: Python uses the key directly, skipping any intermediate items.
You will use this pattern when processing real data like user profiles, sensor logs, or word frequencies in text analysis. Instead of searching through a list to find 'carol', you ask the dictionary directly and get her score instantly.
Looping over items
scores = {'alice': 85, 'bob': 92}
for name, score in scores.items():
print(f'{name}: {score}')dict.items() yields pairs of keys and values. This lets you unpack both at once inside a loop. If you only need the labels, use .keys(). If you only care about the contents, use .values()..get(): safe lookup with a default
inventory = {'apples': 5}
print(inventory.get('bananas', 0)) # prints 0
# print(inventory['bananas']) # would raise KeyErrorWhen a key might be missing, square brackets crash your program.
.get() returns the value if present, or your chosen fallback otherwise..update(): merge another dict
config = {'debug': False, 'port': 80}
config.update({'debug': True, 'timeout': 30})
print(config)update() takes another dictionary and merges it into the current one. Existing keys are overwritten; new ones are added.Counting occurrences
words = ['cat', 'dog', 'cat']
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1
print(counts)This pattern is the foundation of frequency analysis. For each item, you check if it exists (defaulting to zero), then increment its count.
Deleting keys
d = {'a': 1, 'b': 2}
del d['a'] # removes key 'a'
removed = d.pop('b')# returns value and deletes
print(removed)del is a statement that removes the entry. .pop() is a method that both retrieves the value and deletes it, making it useful when you need to use the data before it disappears.