Skip to content

Instantly share code, notes, and snippets.

@stesh
Created February 11, 2014 03:28
Show Gist options
  • Save stesh/8928821 to your computer and use it in GitHub Desktop.
Save stesh/8928821 to your computer and use it in GitHub Desktop.
Instead of defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(float))))
#!/usr/bin/python
from collections import defaultdict
def nested_defaultdict(f, n):
if n == 1:
return defaultdict(f)
else:
return defaultdict(lambda: nested_defaultdict(f, n-1))
q = nested_defaultdict(float, 4)
print q[1][2][3][4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment