Dynamic tree in Python

Content:

Saw this on LinkedIn. Pretty cool trick.

from collections import defaultdict

tree = lambda: defaultdict(tree)

t = tree()
t['root']['a'] = 123
t['root']['b']['c'] = 456
t['root']['b']['d'] = 789
print(t['root']['a'])
print(t['root']['b']['c'])
print(t['root']['b']['d'])

Comments: