from functools import partial
from collections import defaultdict
tripledict = partial(defaultdict, partial(defaultdict, dict))
mydict = tripledict()
mydict['foo']['bar']['foobar'] = 25
Set nested dict value and create intermediate keys