Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
Set Python dict items recursively, when given a compound key 'foo.bar.baz'

I'd like to achieve the following:

foodict['foo.bar.baz'] = 'foo'
{
   'foo': {
      'bar': {
            'baz': 'foo'
         }
       }
   }
}

...creating keys recursively.

After scratching my head for a while, I've come up with this:

class Config(dict):
    def __init__(self, *args, **kwargs):
        self.super = super(Config, self)
        self.update(*args, **kwargs)

    def __setitem__(self, keys, value):
        keys   = keys.split('.')
        keys.reverse()

        config = Config()

        for i, k in enumerate(keys):
            if i == 0:
                config  = Config(**{ k: value })
            else:
                config  = Config(**{ k: config })

        self.super.update(config)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You might consider the "infinite defaultdict" recipe, from Raymond Hettinger himself:

https://twitter.com/raymondh/status/343823801278140417

>>> from collections import defaultdict
>>> infinite_defaultdict = lambda: defaultdict(infinite_defaultdict)
>>> d = infinite_defaultdict()
>>> d['foo']['bar']['baz'] = 'foo'
>>> d
defaultdict(<function <lambda> at 0x1040388c8>, {'foo': defaultdict(<function <lambda> at 0x1040388c8>, {'bar': defaultdict(<function <lambda> at 0x1040388c8>, {'baz': 'foo'})})})

Another option is to implement __missing__:

>>> class InfiniteDict(dict):
...     def __missing__(self, val):
...         d = InfiniteDict()
...         self[val] = d
...         return d
...
>>> d = InfiniteDict()
>>> d['foo']['bar']['baz'] = 'foo'
>>> d
{'foo': {'bar': {'baz': 'foo'}}}

And if you must have attribute access:

class InfiniteDict(dict):
   def __missing__(self, val):
       d = InfiniteDict()
       self[val] = d
       return d
   def __getattr__(self, item):
       return self.__getitem__(item)
   def __setattr__(self, item, value):
       super().__setitem__(item, value)

In action:

>>> d = InfiniteDict()
>>> d.foo.bar.baz = 'foo'
>>> d
{'foo': {'bar': {'baz': 'foo'}}}
>>>

Although, that's a little quick-and-dirty, so no guarantees there aren't any bugs. And there are very little guards against, for example, collisions with actual attributes:

>>> d.keys = 'should I be allowed?'
>>> d
{'foo': {'bar': {'baz': 'foo'}}, 'keys': 'should I be allowed?'}
>>> d.keys()
dict_keys(['foo', 'keys'])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!