Skip to content

Instantly share code, notes, and snippets.

@iamsk
Created April 4, 2016 03:22
Show Gist options
  • Save iamsk/92bed0bc3a369e4b4ad131347e2f2327 to your computer and use it in GitHub Desktop.
Save iamsk/92bed0bc3a369e4b4ad131347e2f2327 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def defaultdict2dict(d):
_d = {}
for k, v in d.items():
if isinstance(v, defaultdict):
_d[k] = defaultdict2dict(v)
else:
_d[k] = v
return _d
if __name__ == "__main__":
temp = defaultdict(lambda: defaultdict(lambda: defaultdict()))
temp[1][2][3] = 1
temp[2][3] = 1
temp[3] = 1
assert isinstance(temp, defaultdict)
assert isinstance(defaultdict2dict(temp), dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment