I have the below original dict:
{'data_extraction': {'if_extraction': False,
'path_data': 'data_extraction/extractions.sql',
'set_params': {'START_DT': "'201901'",
'END_DT': "'202104'"
},
'calibration': True,
'target_variable': 'unsure'}}
I'd like to replace the set params bit with the following dict:
dict1= {'set_params': {'START_DT': "'201001'",
'END_DT': "'201004'"
}}
I took this code from a similar question:
import collections.abc
def update(d, u):
for k, v in u.items():
if isinstance(v, collections.abc.Mapping):
d[k] = update(d.get(k, {}), v)
else:
d[k] = v
return d
update(original,dict1)
Applied it to the above and I got:
{'data_extraction': {'if_extraction': False,
'path_data': 'data_extraction/extractions.sql',
'set_params': {'START_DT': "'201901'",
'END_DT': "'202104'"
},
'calibration': True,
'target_variable': 'unsure'},
'set_params': {'START_DT': "'201001'",
'END_DT': "'201004'" }}
So rather than replace, it appended the replacement dict at the end. How can I update values in a dict such that if I pass another dict as I have with dict1 it replaces it? What am I doing wrong?