from typing import Any
from typing import List
from typing import Union


def get_value_by_path(target: Any, path: List[Union[str, int]]) -> Any:
    """
    Gets the value at path of dict or list.

    :Example:

    .. code-block:: pycon

        In [0]: target = { 'a': { 'b': { 'c': 3 } } }
        Out[0]: {'a': [{'b': {'c': 'AAA'}}]}

        In [1]: target = { 'a': [{ 'b': { 'c': 3 } }] }

        In [2]: get_value_by_path(target, ["a", 0, "b", "c"])
        Out[2]: 3

    The behavior of the function is similar to:
    https://lodash.com/docs#get
    """
    result = target
    for segment in path:
        if isinstance(result, dict):
            result = result[segment]
        elif isinstance(result, list):
            result = result[int(segment)]
        else:
            raise Exception(f"Invalid path: {path}")
    return result