Skip to content

Instantly share code, notes, and snippets.

@white-gecko
Created July 8, 2015 10:56
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 white-gecko/595b22e9e8f3e0d12667 to your computer and use it in GitHub Desktop.
Save white-gecko/595b22e9e8f3e0d12667 to your computer and use it in GitHub Desktop.
Python Default Dictionary of Dictionaries
#! /usr/bin/env python3
from collections import defaultdict
def ddict ():
return defaultdict(ddict)
def ddict2dict(d):
for k, v in d.items():
if isinstance(v, dict):
d[k] = ddict2dict(v)
return dict(d)
myddict = ddict()
myddict["a"]["b"]["c"] = "value"
print(myddict)
mydict = ddict2dict(myddict)
print(mydict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment