dict
nested_dict_delete(root, key, sep='.')
Iterate through a dict, deleting items recursively based on a key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root |
dict
|
dictionary to remove an entry from |
required |
key |
str
|
the string used to locate the key to delete in the root dictionary |
required |
sep |
str
|
the separator for dictionary key items |
'.'
|
Returns:
Name | Type | Description |
---|---|---|
dict |
the modified dict_to |
Examples:
>>> d1 = {'test': {'test_val': 3}}
>>> d2 = {'test': {'test_val': 5, 'test_val_2': 7}, 'other': 3}
>>> nested_dict_delete(d1, 'test.test_val')
>>> d1
{}
>>> nested_dict_delete(d2, 'test.test_val')
>>> d2
{'test': {'test_val_2': 7}, 'other': 3}
Source code in chmpy/util/dict.py
recursive_dict_update(dict_to, dict_from)
Iterate through a dictionary , updating items inplace recursively from a second dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_to |
dict
|
the first dictionary (to update) |
required |
dict_from |
dict
|
the second dictionary (to pull updates from) |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
the modified dict_to |
Examples:
>>> d1 = {'test': {'test_val': 3}}
>>> d2 = {'test': {'test_val': 5}, 'other': 3}
>>> recursive_dict_update(d1, d2)
{'test': {'test_val': 5}, 'other': 3}