Skip to content

Dictionary Utilities

The rite.collections.dict subpackage provides utilities for working with nested dictionaries, merging, filtering, and inverting mappings.

Dictionary Operations Module

Utilities for dictionary manipulation and transformation.

Functions

  • dict_merge: Merge multiple dictionaries.
  • dict_filter: Filter dictionary by predicate.
  • dict_invert: Swap keys and values.
  • dict_deep_get: Get nested value safely.
  • dict_deep_set: Set nested value, creating paths.

Examples

from rite.collections.dict import dict_merge, dict_filter dict_merge({"a": 1}, {"b": 2}) {'a': 1, 'b': 2} dict_filter({"a": 1, "b": 2}, lambda k, v: v > 1)

Modules

dict_deep_get

Dict Deep Get

Safely get nested dictionary values using key paths.

Functions
  • dict_deep_get: Get nested value using list of keys.
Examples

from rite.collections.dict import dict_deep_get data = {"user": {"profile": {"name": "John"}}} dict_deep_get(data, ["user", "profile", "name"]) 'John'

Functions

dict_deep_get(d: dict[str, Any], keys: list[str], default: Any = None) -> Any

Get nested dictionary value using key path.

Parameters:

Name Type Description Default
d dict[str, Any]

Dictionary to traverse.

required
keys list[str]

List of keys forming path to desired value.

required
default Any

Value to return if path doesn't exist.

None

Returns:

Type Description
Any

Value at the key path or default.

Examples:

>>> data = {"user": {"profile": {"name": "John"}}}
>>> dict_deep_get(data, ["user", "profile", "name"])
'John'
>>> dict_deep_get(data, ["user", "missing"], "N/A")
'N/A'
>>> dict_deep_get({}, ["a", "b"])
None

dict_deep_set

Dict Deep Set

Set nested dictionary values, creating intermediate dicts as needed.

Functions
  • dict_deep_set: Set value at nested key path.
Examples

from rite.collections.dict import dict_deep_set d = {} dict_deep_set(d, ["user", "profile", "name"], "John") d {'user': {'profile': {'name': 'John'}}}

Functions

dict_deep_set(d: dict[str, Any], keys: list[str], value: Any) -> None

Set nested dictionary value, creating intermediate dicts.

Modifies dictionary in-place.

Parameters:

Name Type Description Default
d dict[str, Any]

Dictionary to modify.

required
keys list[str]

List of keys forming path where value should be set.

required
value Any

Value to set at the key path.

required

Examples:

>>> d = {}
>>> dict_deep_set(d, ["user", "profile", "name"], "John")
>>> d
{'user': {'profile': {'name': 'John'}}}
>>> dict_deep_set(d, ["user", "age"], 30)
>>> d['user']['age']
30

dict_filter

Dict Filter

Filter dictionary entries based on key or value predicates.

Functions
  • dict_filter: Filter dictionary by predicate.
Examples

from rite.collections.dict import dict_filter dict_filter({"a": 1, "b": 2, "c": 3}, lambda k, v: v > 1)

Functions

dict_filter(d: dict[K, V], predicate: Callable[[K, V], bool]) -> dict[K, V]

Filter dictionary entries based on predicate function.

Parameters:

Name Type Description Default
d dict[K, V]

Dictionary to filter.

required
predicate Callable[[K, V], bool]

Function taking (key, value) returning True to keep.

required

Returns:

Type Description
dict[K, V]

New dictionary with filtered entries.

Examples:

>>> dict_filter({"a": 1, "b": 2, "c": 3}, lambda k, v: v > 1)
{'b': 2, 'c': 3}
>>> dict_filter({"a": 1, "b": 2}, lambda k, v: k == "a")
{'a': 1}
>>> dict_filter({}, lambda k, v: True)
{}

dict_invert

Dict Invert

Invert dictionary keys and values.

Functions
  • dict_invert: Swap dictionary keys and values.
Examples

from rite.collections.dict import dict_invert dict_invert({"a": 1, "b": 2})

Functions

dict_invert(d: dict[K, V]) -> dict[V, K]

Invert dictionary keys and values.

Parameters:

Name Type Description Default
d dict[K, V]

Dictionary to invert.

required

Returns:

Type Description
dict[V, K]

New dictionary with keys and values swapped.

Raises:

Type Description
TypeError

If values are not hashable.

Examples:

>>> dict_invert({"a": 1, "b": 2})
{1: 'a', 2: 'b'}
>>> dict_invert({1: "one", 2: "two"})
{'one': 1, 'two': 2}
>>> dict_invert({})
{}

dict_merge

Dict Merge

Merge multiple dictionaries with various strategies.

Functions
  • dict_merge: Merge dictionaries with configurable behavior.
Examples

from rite.collections.dict import dict_merge dict_merge({"a": 1, "b": 2}, {"b": 3, "c": 4})

Functions

dict_merge(*dicts: dict[K, V], deep: bool = False) -> dict[K, V]

Merge multiple dictionaries.

Later dictionaries override earlier ones for duplicate keys.

Parameters:

Name Type Description Default
*dicts dict[K, V]

Variable number of dictionaries to merge.

()
deep bool

If True, recursively merge nested dicts.

False

Returns:

Type Description
dict[K, V]

New merged dictionary.

Examples:

>>> dict_merge({"a": 1, "b": 2}, {"b": 3, "c": 4})
{'a': 1, 'b': 3, 'c': 4}
>>> dict_merge({"a": 1}, {"b": 2}, {"c": 3})
{'a': 1, 'b': 2, 'c': 3}
>>> d1 = {"a": {"x": 1}}
>>> d2 = {"a": {"y": 2}}
>>> dict_merge(d1, d2, deep=True)
{'a': {'x': 1, 'y': 2}}

options: show_root_heading: true show_source: false heading_level: 2