from typing import Union
from typing import Sequence


def normalise_index(index: int, length: Union[int, Sequence]) -> int:
    """
    Normalises the given list index into the valid range for a
    list of the given length. This way negative indices index
    backward from the end of the list.

    :param index:   The index to normalise.
    :param length:  The sequence length to normalise to, or a sequence.
    :return:        The normalised index.
    """
    # Get the length if we're given a sequence
    if not isinstance(length, int):
        length = len(length)

    return index % length