update_dict¶
- pyhelpers.ops.update_dict(dictionary, updates, inplace=False)[source]¶
Update a (nested) dictionary with another dictionary.
See also [OPS-UD-1].
- Parameters:
dictionary (dict) – The (nested) dictionary to be updated.
updates (dict) – The dictionary containing updates.
inplace (bool) – Whether to update the original
dictionary
in place; defaults toFalse
.
- Returns:
The updated dictionary.
- Return type:
dict
Examples:
>>> from pyhelpers.ops import update_dict >>> source_dict = {'key_1': 1} >>> update_data = {'key_2': 2} >>> upd_dict = update_dict(source_dict, updates=update_data) >>> upd_dict {'key_1': 1, 'key_2': 2} >>> source_dict {'key_1': 1} >>> update_dict(source_dict, updates=update_data, inplace=True) >>> source_dict {'key_1': 1, 'key_2': 2} >>> source_dict = {'key': 'val_old'} >>> update_data = {'key': 'val_new'} >>> upd_dict = update_dict(source_dict, updates=update_data) >>> upd_dict {'key': 'val_new'} >>> source_dict = {'key': {'k1': 'v1_old', 'k2': 'v2'}} >>> update_data = {'key': {'k1': 'v1_new'}} >>> upd_dict = update_dict(source_dict, updates=update_data) >>> upd_dict {'key': {'k1': 'v1_new', 'k2': 'v2'}} >>> source_dict = {'key': {'k1': {}, 'k2': 'v2'}} >>> update_data = {'key': {'k1': 'v1'}} >>> upd_dict = update_dict(source_dict, updates=update_data) >>> upd_dict {'key': {'k1': 'v1', 'k2': 'v2'}} >>> source_dict = {'key': {'k1': 'v1', 'k2': 'v2'}} >>> update_data = {'key': {'k1': {}}} >>> upd_dict = update_dict(source_dict, updates=update_data) >>> upd_dict {'key': {'k1': 'v1', 'k2': 'v2'}}