Dictionary Methods Deep Dive
Dictionary methods are the built-in functions every dict carries — like get, setdefault, update, pop, and items — that let you read, change, and combine key-value data safely without ever risking a crash from a missing key.
Learn Dictionary Methods Deep Dive in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You already know how to create a dictionary. Now you'll master the toolkit professionals reach for every single day to keep their code short, safe, and readable.
Accessing a missing key with square brackets raises a KeyError and stops your program. The get() method returns a fallback instead, and setdefault() goes one step further by inserting that fallback so you can build values up:
You often need to combine dictionaries — merging defaults with overrides, or folding new data into a record. The classic tool is update() ; since Python 3.9 you also have the cleaner | (merge) and |= (in-place merge) operators:
When keys collide, the right-hand dictionary always wins. That's exactly what you want for "defaults on the left, user choices on the right."
pop(key) removes a key and returns its value (with an optional default so it won't crash). popitem() removes and returns the last inserted pair. And keys() , values() , and items() give you live views for looping:
Replace each ___ so the tally counts every vote safely. Hint: get() with a default of 0 is perfect for counting.
Combine two order batches into one, summing quantities for shared items, then report the busiest item.
Lesson complete — you handle dictionaries like a pro!
You can read safely with get() , build values with setdefault() , merge with update() and | , remove with pop() and popitem() , and loop cleanly over items() — all without a single KeyError or RuntimeError .
🚀 Up next: Counter & defaultdict — specialized dictionaries that make counting and grouping even shorter.
Practice quiz
Given stock = {'apples': 12}, what does stock.get('bananas') return?
- 0
- KeyError
- None
- ''
Answer: None. get() returns None for a missing key instead of raising KeyError.
What does stock.get('bananas', 0) return when 'bananas' is missing?
- 0
- None
- KeyError
- False
Answer: 0. get() returns the supplied default (0) when the key is absent.
What is the key difference between get() and setdefault()?
- They are identical
- get() inserts the key
- setdefault() only reads
- setdefault() inserts the key if missing; get() never changes the dict
Answer: setdefault() inserts the key if missing; get() never changes the dict. get() only reads; setdefault() reads AND inserts the default when the key is missing.
When merging with the | operator, if keys collide which value wins?
- The left-hand dict
- The right-hand dict
- It raises an error
- The smaller value
Answer: The right-hand dict. On key collisions, the right-hand dictionary's value wins.
What does a.update(b) return?
- None (it updates in place)
- The merged dict
- A copy of a
- b
Answer: None (it updates in place). update() mutates the dict in place and returns None.
Counting votes with tally[c] = tally.get(c, 0) + 1 over ['red','blue','red'] gives red = ?
- 1
- 3
- 2
- 0
Answer: 2. 'red' appears twice, so its count ends at 2.
Which raises 'RuntimeError: dictionary changed size during iteration'?
- Looping over list(d.keys()) and deleting
- Deleting keys while looping directly over the dict
- Using a dict comprehension
- Calling d.items()
Answer: Deleting keys while looping directly over the dict. Adding or removing keys while iterating the dict directly raises this RuntimeError.
What does cart.popitem() remove and return?
- The first inserted pair
- A random pair
- The largest value
- The last inserted pair
Answer: The last inserted pair. popitem() removes and returns the last inserted key-value pair (LIFO).
What is the safe way to delete keys while iterating?
Iterate over a snapshot like list(d.keys()) so the live dict can change safely.
Using setdefault, groups.setdefault(first, []).append(name) for ['Ada','Ben','Bea'] groups B as...
- Bea
Both Ben and Bea start with B, so the 'B' list becomes ['Ben', 'Bea'].