defaultdict of defaultdict, nested

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

IOW, if I do:

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

That's what I want. 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.

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 that someone marked this as a duplicate of Python: defaultdict of defaultdict? , but this isn't the same question... that question was how to do a two-level defaultdict; this one is how to do an infinite-level recursive defaultdict.

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: