Generating dictionary keys on the fly [duplicate]
class D(dict):
def __missing__(self, key):
self[key] = D()
return self[key]
d = D()
d['a']['b']['c'] = 3
You could use a tuple as the key for the dict and then you don't have to worry about subdictionaries at all:
mydict[(key,subkey,subkey2)] = "value"
Alternatively, if you really need to have subdictionaries for some reason you could use collections.defaultdict
.
For two levels this is straightforward:
>>> from collections import defaultdict
>>> d = defaultdict(dict)
>>> d['key']['subkey'] = 'value'
>>> d['key']['subkey']
'value'
For three it's slightly more complex:
>>> d = defaultdict(lambda: defaultdict(dict))
>>> d['key']['subkey']['subkey2'] = 'value'
>>> d['key']['subkey']['subkey2']
'value'
Four and more levels are left as an exercise for the reader. :-)
I like Dave's answer better, but here's an alternative.
from collections import defaultdict
d = defaultdict(lambda : defaultdict(int))
>>> d['a']['b'] += 1
>>> d
defaultdict(<function <lambda> at 0x652f0>, {'a': defaultdict(<type 'int'>, {'b': 1})})
>>> d['a']['b']
1
http://tumble.philadams.net/post/85269428/python-nested-defaultdicts
It's definitely not pretty to have to use lambdas to implements the inner defaulted collections, but apparently necessary.