Nested defaultdict of defaultdict

Is there a way to make a defaultdict also be the default for the defaultdict?

I want to be able to do:

    x = defaultdict(...stuff...)
    x[0][1][0]
    {}

So, I can do x = defaultdict(defaultdict), but that's only one level:

    x[0]
    {}
    x[0][0]
    KeyError: 0

There are recipes that can do this. But can it be done simply just using the normal defaultdict arguments?

Note this is asking how to do an infinite-level recursive defaultdict, so it's distinct to Python: defaultdict of defaultdict? , which was how to do a two-level defaultdict.

I'll probably just end up using the bunch pattern, but when I realized I didn't know how to do this, it got me interested.

For an arbitrary number of levels:

    def rec_dd():
        return defaultdict(rec_dd)

    >>> x = rec_dd()
    >>> x['a']['b']['c']['d']
    defaultdict(<function rec_dd at 0x7f0dcef81500>, {})
    >>> print json.dumps(x)
    {"a": {"b": {"c": {"d": {}}}}}

Of course you could also do this with a lambda, but I find lambdas to be less readable. In any case it would look like this:

    rec_dd = lambda: defaultdict(rec_dd)

From: stackoverflow.com/q/19189274

Back to homepage or read more recommendations: