Last active
March 21, 2018 01:08
-
-
Save adeishs/0fdbc7ba8b0bd86f5b9cc9c11543d100 to your computer and use it in GitHub Desktop.
Multilevel Dictionary (Solution)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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