def nested_unpair(
    d: Dict[AnyKey, Any], key_paths: Tuple[List[AnyKey], ...], labels: Tuple[str, str]
) -> Tuple[Dict[AnyKey, Any], Dict[AnyKey, Any]]:
    """
    Form two dictionaries out of the initial dictionary `d`. In correspondence to the sequences of keys in `key_paths`,
     the first and second dictionary will get the values marked by `labels`. Otherwise, the first dictionary takes the
     values of `d`, while the second dictionary does not take any.

    :param d: Dict[AnyKey, Any]
        A nested dictionary.
    :param key_paths: Tuple[List[AnyKey]]
        Each element of the tuple is a sequence of keys defining the path to an item of the nested dictionary. In
        correspondence to each key path, the value must be a dictionary with `labels` as keys.
    :param labels: Tuple[str, str]
        Labels indicating the keys of the objects going into the first and second dictionaries, respectively.

    :return Tuple[Dict[AnyKey, Any], Dict[AnyKey, Any]]
        The two dictionaries.
    """
    if type(key_paths) != tuple:
        raise TypeError("`key_paths` must be a tuple.")
    if len(labels) != 2:
        raise ValueError("The length of `labels` must be exactly 2.")

    d01 = deepcopy(d)
    d02 = dict()

    for keys in key_paths:
        error_msg = f"The sequence `keys={keys}` was not found in `d`."
        d21 = d01
        d22 = d02
        for key in keys[:-1]:
            if key in d21:
                d21 = d21[key]
                if key not in d22:
                    d22[key] = dict()
                d22 = d22[key]
            else:
                raise KeyError(error_msg)
        if keys[-1] in d21:
            d22[keys[-1]] = d21[keys[-1]][labels[1]]
            d21[keys[-1]] = d21[keys[-1]][labels[0]]
        else:
            raise KeyError(error_msg)

    return d01, d02

    Form two dictionaries out of the initial dictionary `d`. In correspondence to the sequences of keys in `key_paths`,
     the first and second dictionary will get the values marked by `labels`. Otherwise, the first dictionary takes the
     values of `d`, while the second dictionary does not take any.

    :param d: Dict[AnyKey, Any]
        A nested dictionary.
    :param key_paths: Tuple[List[AnyKey]]
        Each element of the tuple is a sequence of keys defining the path to an item of the nested dictionary. In
        correspondence to each key path, the value must be a dictionary with `labels` as keys.
    :param labels: Tuple[str, str]
        Labels indicating the keys of the objects going into the first and second dictionaries, respectively.

    :return Tuple[Dict[AnyKey, Any], Dict[AnyKey, Any]]
        The two dictionaries.
    
fortuna/utils/nested_dicts.py