dictutils.ops.map_itemsΒΆ

dictutils.ops.map_items(d, fn, *, deep=False)[source]ΒΆ

Transform both keys and values in a mapping using a function.

Parameters:
  • d (Mapping[str, Any]) – Mapping to transform

  • fn (Callable[[str, Any], tuple[str, Any]]) – Function that takes (key, value) and returns (new_key, new_value)

  • deep (bool) – Whether to recursively transform nested mappings

Return type:

dict[str, Any]

Returns:

New mapping with transformed items

Example

>>> import json
>>> data = {"count": 5, "total": 100}
>>> result = map_items(data, lambda k, v: (f"{k}_value", v * 10))
>>> print(json.dumps(result, indent=4))
{
    "count_value": 50,
    "total_value": 1000
}