dictutils.ops.deep_update

dictutils.ops.deep_update(a, b, *, dict_strategy='merge', list_strategy='extend', unique_by=None, by_key=None, scalar_strategy='replace')[source]

Strategy-aware deep update of object a with values from object b.

Parameters:
  • a (Any) – Target object to update

  • b (Any) – Source object to merge from

  • dict_strategy (str) – How to handle dict merging (“merge” or “replace”)

  • list_strategy (str) – How to handle list merging (“extend”, “replace”, “unique”, “by_key”)

  • unique_by (Optional[Callable[[Any], Any]]) – Function to extract unique key for “unique” strategy

  • by_key (Union[str, Callable[[Any], Any], None]) – Key or function for “by_key” strategy

  • scalar_strategy (str) – How to handle scalar conflicts (“replace”, “keep_first”, “keep_last”)

Return type:

Any

Returns:

The updated object a

Example

>>> import json
>>> data1 = {"users": [{"id": 1, "name": "Alice"}], "config": {"debug": True}}
>>> data2 = {"users": [{"id": 2, "name": "Bob"}], "config": {"port": 8080}}
>>> deep_update(data1, data2)
>>> print(json.dumps(data1, indent=4))
{
    "users": [
        {
            "id": 1,
            "name": "Alice"
        },
        {
            "id": 2,
            "name": "Bob"
        }
    ],
    "config": {
        "debug": true,
        "port": 8080
    }
}