defaultdict defaultdict auto-creates missing keys with a factory function. from collections import defaultdict counts = defaultdict(int) counts['a'] += 1 Learned on 2025-05-20