Skip to content

Dicts

  • Dictionary Utils

dict_first(d)

Extract the first key, value pair from a dictionary. @param d: Dictionary to retrieve first key, value pair from. @return: Tuple containing key, value.

Source code in src/utils/dicts.py
66
67
68
69
70
71
72
def dict_first(d: dict) -> tuple[Hashable, Any]:
    """
    Extract the first key, value pair from a dictionary.
    @param d: Dictionary to retrieve first key, value pair from.
    @return: Tuple containing key, value.
    """
    return next(iter(d.items()))

dict_last(d)

Extract the last key, value pair from a dictionary. @param d: Dictionary to retrieve last key, value pair from. @return: Tuple containing key, value.

Source code in src/utils/dicts.py
75
76
77
78
79
80
81
def dict_last(d: dict) -> tuple[Hashable, Any]:
    """
    Extract the last key, value pair from a dictionary.
    @param d: Dictionary to retrieve last key, value pair from.
    @return: Tuple containing key, value.
    """
    return list(d.items())[-1]

dict_sort_by_key(d, reverse=False)

Sort a dictionary by its key. @param d: Dictionary to sort by its key. @param reverse: Whether to reverse the sorting order. @return: Key sorted dictionary.

Source code in src/utils/dicts.py
41
42
43
44
45
46
47
48
def dict_sort_by_key(d: dict, reverse: bool = False) -> dict:
    """
    Sort a dictionary by its key.
    @param d: Dictionary to sort by its key.
    @param reverse: Whether to reverse the sorting order.
    @return: Key sorted dictionary.
    """
    return dict(sorted(d.items(), reverse=reverse))

dict_sort_by_val(d, reverse=False)

Sort a dictionary by its value. @param d: Dictionary to sort by its value. @param reverse: Whether to reverse the sorting order. @return: Value sorted dictionary.

Source code in src/utils/dicts.py
51
52
53
54
55
56
57
58
def dict_sort_by_val(d: dict, reverse: bool = False) -> dict:
    """
    Sort a dictionary by its value.
    @param d: Dictionary to sort by its value.
    @param reverse: Whether to reverse the sorting order.
    @return: Value sorted dictionary.
    """
    return dict(sorted(d.items(), key=lambda item: item[1], reverse=reverse))

reverse_dict(d)

Flips the key, val in a dictionary to val, key. @param d: Dictionary where the values are hashable. @return: Reversed dictionary.

Source code in src/utils/dicts.py
11
12
13
14
15
16
17
18
19
20
def reverse_dict(d: dict[Hashable, Hashable]) -> dict[Hashable, Hashable]:
    """
    Flips the key, val in a dictionary to val, key.
    @param d: Dictionary where the values are hashable.
    @return: Reversed dictionary.
    """
    inverted = {}
    for k, v in d.items():
        inverted.setdefault(v, k)
    return inverted

reverse_dict_safe(d)

Flips the key, val in a dictionary to val, [keys], preserving cases where the same value is mapped to multiple keys. @param d: Dictionary where the values are hashable. @return: Reversed dictionary.

Source code in src/utils/dicts.py
23
24
25
26
27
28
29
30
31
32
33
def reverse_dict_safe(d: dict[Hashable, Hashable]) -> dict[Hashable, Hashable]:
    """
    Flips the key, val in a dictionary to val, [keys], preserving cases where the same
    value is mapped to multiple keys.
    @param d: Dictionary where the values are hashable.
    @return: Reversed dictionary.
    """
    inverted = {}
    for k, v in d.items():
        inverted.setdefault(v, []).append(k)
    return inverted