from typing import Dict
from typing import List
from typing import Any


def set_dict_value(dictionary: Dict, keys: List[Any], value: Any) -> Dict:
    """
    Set a value in a (nested) dictionary by defining a list of keys.

    .. note:: Side-effects
              This function does not make a copy of dictionary, but directly
              edits it.

    Parameters
    ----------
    dictionary : Dict
    keys : List[Any]
    value : Any

    Returns
    -------
    dictionary : dict

    Examples
    --------
    >>> d = {'a': {'b': {'c': 'x', 'f': 'g'}, 'd': 'e'}}
    >>> expected = {'a': {'b': {'c': 'foobar', 'f': 'g'}, 'd': 'e'}}
    >>> set_dict_value(d, ['a', 'b', 'c'], 'foobar') == expected
    True
    """
    orig = dictionary
    for key in keys[:-1]:
        dictionary = dictionary.setdefault(key, {})
    dictionary[keys[-1]] = value
    return orig