I'm looking for a way to update a nested dict dict1
dic1 = {'key1': {'key2': {'key3': {'key4': {'key51': {'key511': 'val511', 'key512': 'val512'}}, 'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}}}}}
I want to replace the element in dic1: {'key51': {'key511': 'val511', 'key512': 'val512'}} to 'val4'
So I can get a new dictionary as the following:
dic2 = {'key1': {'key2': {'key3': {'key4': 'val4', 'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}}}}}
I can do this manually using the following statement
>>> dic1['key1']['key2']['key3']['key4'] = 'val4'
but I got stuck in writing Python code for this problem
Your help would be very much appreciated. Thank you.
dic1 = {'key1': {'key2': {'key3': {'key4': {'key51': {'key511': 'val511', 'key512': 'val512'}}, 'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}}}}}
I want to replace the element in dic1: {'key51': {'key511': 'val511', 'key512': 'val512'}} to 'val4'
So I can get a new dictionary as the following:
dic2 = {'key1': {'key2': {'key3': {'key4': 'val4', 'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}}}}}
I can do this manually using the following statement
>>> dic1['key1']['key2']['key3']['key4'] = 'val4'
Code:
>>> dic1
{'key1': {'key2': {'key3': {'key52': {'key521': {'key5211': 'val5211', 'key5212': 'val5212'}}, 'key4': 'val4'}}}}
Your help would be very much appreciated. Thank you.
Comment