Skip to content

API reference

earthcarekit.utils.dict

Dictionary utilities.

Notes

This module does not depend on other internal modules.


invert_dict

invert_dict(d: dict[A, B]) -> dict[B, A]

Return new dictionary with keys and values swapped (assumes all unique values).

Source code in earthcarekit/utils/dict.py
def invert_dict(d: dict[A, B]) -> dict[B, A]:
    """Return new dictionary with keys and values swapped (assumes all unique values)."""
    return {v: k for k, v in d.items()}

invert_dict_nonunique

invert_dict_nonunique(d: dict[A, B]) -> dict[B, list[A]]

Return new dictionary mapping from values to lists of keys that map to it.

Source code in earthcarekit/utils/dict.py
def invert_dict_nonunique(d: dict[A, B]) -> dict[B, list[A]]:
    """Return new dictionary mapping from values to lists of keys that map to it."""
    inv_d: dict[B, list[A]] = {}
    for k, v in d.items():
        inv_d.setdefault(v, []).append(k)
    return inv_d

remove_keys_from_dict

remove_keys_from_dict(d: dict[A, B], keys: list[A]) -> dict[A, B]

Return new dictionary with selected keys removed.

Source code in earthcarekit/utils/dict.py
def remove_keys_from_dict(d: dict[A, B], keys: list[A]) -> dict[A, B]:
    """Return new dictionary with selected keys removed."""
    d = d.copy()
    for k in keys:
        if k in d:
            del d[k]
    return d