rupdate#
- tollan.utils.dict.rupdate(d: MutableMapping[Any, Any] | MutableSequence[Any], u: Mapping[Any, Any], *, copy_subdict: bool = True) None[source]#
Update dict recursively.
This will update
dwith items inuin a recursive fashion.dcan be either list or dict.uhas to be dict where int keys can be used to identify list items.When updating list, slice notation in brackets can be used for slice-based modifications following Python’s native list slice syntax.
- Parameters:
- Returns:
None – Dict
dis updated in place.List DSL Syntax
—————
Integer keys (0, 1, -1) – Update value at index (e.g., {0: ‘new’} replaces first item)
Slice notation keys – “[:]” : Replace entire list “[:0]” : Prepend to list “[N:M]” : Replace slice [N:M] (empty list deletes elements) “[N:N]” : Insert at position N “[]” : Extend list at end (special case)
Chaining Operations – Multiple operations can be combined in a single rupdate call. Operations are applied in dict iteration order (Python 3.7+). Index-based operations refer to the current state after previous operations have been applied.
Examples
>>> d = {"items": [1, 2, 3]} >>> rupdate(d, {"items": {"[]": 4}}) # Extend at end >>> d["items"] [1, 2, 3, 4]
>>> d = {"items": [1, 2, 3]} >>> rupdate(d, {"items": {"[:0]": 0}}) # Prepend >>> d["items"] [0, 1, 2, 3]
>>> d = {"items": [1, 2, 3, 4]} >>> rupdate(d, {"items": {"[1:3]": [10, 20]}}) # Replace slice >>> d["items"] [1, 10, 20, 4]
>>> d = {"items": [1, 2, 3, 4]} >>> rupdate(d, {"items": {"[1:3]": []}}) # Delete slice >>> d["items"] [1, 4]
>>> d = {"items": [1, 2, 3]} >>> rupdate(d, {"items": {0: 10}}) # Update at index >>> d["items"] [10, 2, 3]
>>> d = {"items": [1, 2, 3]} >>> rupdate(d, {"items": {"[]": [4, 5], -1: 10}}) # Chain: extend then update >>> d["items"] [1, 2, 3, 4, 10]
Notes
Follows Python’s native list slice assignment behavior.
Operation Ordering: Since Python 3.7+, dict insertion order is preserved. Operations are applied in the order they appear in the update dict. When chaining operations, index-based operations (like -1) refer to the list state after previous operations have been applied.
See [1].