dictutils.ops.deep_setΒΆ

dictutils.ops.deep_set(obj, path, value, *, create_missing=True, create_mapping=<class 'dict'>)[source]ΒΆ

Set value in nested object using dotted path notation.

Parameters:
  • obj (Any) – The object to modify

  • path (Union[str, Sequence[Union[str, int]]]) – Path as string (β€œa.b.c”) or sequence [β€œa”, β€œb”, β€œc”]

  • value (Any) – Value to set at the path

  • create_missing (bool) – Whether to create missing intermediate paths

  • create_mapping (Callable[[], Any]) – Factory function for creating new mappings

Return type:

Any

Returns:

The modified object

Example

>>> import json
>>> data = {}
>>> deep_set(data, "user.profile.name", "Alice")
>>> print(json.dumps(data, indent=4))
{
    "user": {
        "profile": {
            "name": "Alice"
        }
    }
}
>>> deep_set(data, "user.profile.tags[0]", "admin")
>>> print(json.dumps(data, indent=4))
{
    "user": {
        "profile": {
            "name": "Alice",
            "tags": [
                "admin"
            ]
        }
    }
}