0

Hi I have dictionary formatted in that way:

 dictionary = {'aaa.bbb.ccc': 1, 
               'aaa.bbb.ddd.eee': 2, 
               'aaa.bbb.fff': 3, 
               'aaa.ggg.hhh': 4, 
               'aaa.ggg.iii': 5, 
               'jjj.kkk.lll.mmm': 6, 
               'jjj.kkk.lll.nnn': 7}

Is there a easy way to format it to nested dictionary, as it looks below:

nested_dict = {'aaa': 
                    {'bbb':
                          {'ccc':1,
                           'ddd':
                                {'eee':2},
                           'fff':3},
                     'ggg':
                          {'hhh':4,
                           'iii':5}}, 
               'jjj': 
                    {'kkk':
                          {'lll':
                                {'mmm': 6,
                                 'nnn': 7}}}}

I tried to split it in dots place and creating new dict layer by layer, but that dictionary have different nest level so there is a lot of exceptions to overcome. That's why I'm searching for faster and better solution.

sluwer
  • 11
  • 3

0 Answers0