>>> from collections import defaultdict >>> d = {'foo': 123, 'bar': 456} >>> d['baz'] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'baz' >>> d = defaultdict(lambda: -1, d) >>> d['baz'] -1