Is there a way to make a defaultdict also be the default for the defaultdict? (i.e. infinite-level recursive defaultdict?)
I want to be able to do:
x
The other answers here tell you how to create a defaultdict
which contains "infinitely many" defaultdict
, but they fail to address what I think may have been your initial need which was to simply have a two-depth defaultdict.
You may have been looking for:
defaultdict(lambda: defaultdict(dict))
The reasons why you might prefer this construct are:
defaultdict
to be something other than a dictionary, e.g.,: defaultdict(lambda: defaultdict(list))
or defaultdict(lambda: defaultdict(set))