def get_ancestor_keys(key, candidates):
    """
    Gets all ancestors for a key among a list of candidates.

    Parameters
    ----------
    key: str
        The keys who ancestors we want.

    candidates: list of str
        The candidate ancestors.

    Output
    ------
    ancestors: list of str
        The ancestors ordered so that the oldest ancestors come first.
    """
    # drop any duplicates
    candidates = list(dict.fromkeys(candidates))

    # pull out all ancestors
    ancestors = []
    for maybe_ancestor in candidates:
        # a key startes with all its ancestors
        if key.startswith(maybe_ancestor):
            ancestors.append(maybe_ancestor)

    # order ancestors so the oldest ancestors come first
    ancestors = sorted(ancestors, key=lambda s: len(s))

    return ancestors