Search code examples
pythondictionarynestedshorthand

Convenient way to handle deeply nested dictionary in Python


I have a deeply nested dictionary in python thats taking up a lot of room. Is there a way to abbreviate something like this

master_dictionary['sub_categories'][sub_cat_name]['attributes'][attribute_name]['special_type']['nested_children'][child_cat_name][color] = blue

to this for example

nested_child_info[color] = blue

And still have it edit the dictionary ? I hope that makes sense.


Solution

  • nested_child_info = master_dictionary['sub_categories'][sub_cat_name]['attributes'][attribute_name]['special_type']['nested_children'][child_cat_name]
    nested_child_info[color] = blue
    

    nested_child_info is a reference, so changing its contents is going to change the contents of master_dictionary.