I have an object that has an unknown depth and width at every depth. Sort of like a path but it's a different type of object.
My goal is to create a nested dictionary that represents this structure.
It starts as one item at the top of the hierarchy and I can grab the elements below it using a get_items() function. Also, if I call str() on the object it returns the name of the object.
For example:
str(object) = 'top hierarchy'
object.get_items() returns a list ~ [object_level1_part1, object_level1_part2]
str(object_level1_part1) = 'level1_part1'
object_level1_part1.get_items() returns a list ~ [object_level2_part1]
str(object_level2_part2) = 'level2_part1'
etc... There is no guarantee at any level how any objects in the list there will be. Also, there is no guarantee how deep any level might go. However, there is probably nothing greater than a depth of 10.
I want to take this object & recursively search it in order to create a dictionary that would look like something below:
result_dict = {'top_hierarchy':{'level1_part1':{'level2_part1':{}}, 'level1_part2':{}}}
One of the challenges I am having is to continually add to the dictionary in a nested fashion I have to specify previous keys.
result_dict['top_hierarchy']['level1_part1']['level2_part1'] = {'level3_part1':{}}
Is there a way to create a nested dictionary recursively with unknown depth and width?