You can use recursion for this. Like this:
data = {'personal_information': {'name' : {'first_name': 'Ashutosh'}}}
path_to_add = ['personal_information', 'address', 'state']
value = 'Delhi'
def addValue(dictionary, path, value):
if len(path) > 1:
if path[0] not in dictionary.keys():
dictionary[path[0]] = {}
addValue(dictionary[path[0]], path[1:], value)
else:
dictionary[path[0]] = value
print(data)
addValue(data, path_to_add, value)
print(data)
If you are interested to learn Python from Industry experts, you can sign up for this Python Certification Course by Intellipaat.