PyMasterPython trace & interview prep
← Home

Python Dictionary Tracing Problems for Beginners

Dictionaries test insertion order, key collisions, and view objects. Google-style questions often nest dicts with list values.

Example problems

Setdefault

d = {}
d.setdefault('a', []).append(1)
d.setdefault('a', []).append(2)
print(d)

Output: {'a': [1, 2]}

Merge

a = {'x': 1}
b = {'x': 2, 'y': 3}
c = {**a, **b}
print(c['x'], len(c))

Output: 2 2

Keys view

d = {'a': 1}
k = d.keys()
d['b'] = 2
print(len(list(k)))

Output: 2

Generate 100+ more problems instantly

AI-powered unique questions for dictionaries at every difficulty.