from collections import defaultdict
counts = defaultdict(int)
words = ["a", "b", "a", "c", "b", "a"]
for w in words:
counts[w] += 1
print(dict(counts))What does this print?
- a{'a': 3, 'b': 2, 'c': 1}✓
- b{'a': 1, 'b': 1, 'c': 1}
- cRaises KeyError on the first access to counts["a"]
- d{'a': 3, 'b': 2, 'c': 1, None: 0}
Explanation:defaultdict(int) supplies 0 (the result of calling int()) for any missing key instead of raising KeyError, so counts[w] += 1 works even the first time a word is seen. No extra keys like None are ever added — only keys that were actually looked up appear.