Skip to content

Instantly share code, notes, and snippets.

@adeishs
Last active March 21, 2018 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeishs/0fdbc7ba8b0bd86f5b9cc9c11543d100 to your computer and use it in GitHub Desktop.
Save adeishs/0fdbc7ba8b0bd86f5b9cc9c11543d100 to your computer and use it in GitHub Desktop.
Multilevel Dictionary (Solution)
#!/usr/bin/env python
import collections
import pprint
dict_factory = lambda: collections.defaultdict(dict_factory)
x = collections.defaultdict(dict_factory)
x['a']['b']['c']['d'] = 1
print x['a']['b']['c']['d']
print pprint.pformat(x) # looks a bit messy, but it gives you the idea
"""
Sample output (the addresses [in hexadecimal] will likely be different):
1
defaultdict(<function <lambda> at 0x7f733afa6230>, {'a': defaultdict(<function <lambda> at 0x7f733afa6230>, {'b': defaultdict(<function <lambda> at 0x7f733afa6230>, {'c': defaultdict(<function <lambda> at 0x7f733afa6230>, {'d': 1})})})})
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment