After reading What is the best way to implement nested dictionaries? why is it wrong to do:
c = collections.defaultdict(collections.defaultdict(int))
in python? I would think this would work to produce
{key:{key:1}}
or am I thinking about it wrong?
The constructor of
defaultdictexpects a callable.defaultdict(int)is a default dictionary object, not a callable. Using alambdait can work, however:This works since what I pass to the outer
defaultdictis a callable that creates a newdefaultdictwhen called.Here’s an example: