Welcome, guest | Sign In | My Account | Store | Cart

A nested dictionary which creates subnotes on the fly.

Python, 9 lines
1
2
3
4
5
6
7
8
9
class auto_dict(dict):
    def __getitem__(self, key):
        return self.setdefault(key, self.__class__())


if __name__ == "__main__":
    d = auto_dict()
    d["foo"]["bar"]["baz"] = 42
    print d

After reading an article about autovivication hashes in Ruby, I tried to find a simpler solution for Python.

I want a nested dictionary like objects, which can be indexed in multiple dimensions and which elements are automatically created, when accessed.

The implementation I used before is about 10 lines of code, this one is shorter and imho more elegant.

It used the setdefault() method of a dictionay to add another instance of self.__class__ as the default value for every key.

Created by Mirko Dziadzka on Wed, 19 Dec 2007 (PSF)
Python recipes (4591)
Mirko Dziadzka's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks