Sorting Exclusive of Specified Indexes in List To further generalize the algorithm (past the doc string): Given a list E(1) E(3) E(2) E(4) _ E(0) _ where E(N + 1) is ahead of E(N) when sorted and _ represents a "static element" (that is, an element whose index and value will remain unchanged despite the sorting), the list returned should be E(0) E(1) E(2) E(3) _ E(4) _.

In the following code, non-static elements would be the elements affected by the sorting, while a static element would be unaffected (i.e, an _).

def static_sort(old_list: list, static_index_list: list) -&gt; list:
    """
    Returns a list whose non-static elements (defined to be the ones whose
    indexes are not specified in the static_index_list) are sorted in ascending order.
    The static elements (with indexes specified in the static_index_list) remain unchanged
    in both numeric value and index in the original list.

    static_sort([1, 9, 3, 5], [0, 2]) -&gt; [1, 5, 3, 9]
    static_sort([0, 8, 2, 6], [2]) -&gt; [0, 6, 2, 8]

    :param static_index_list: A list of indexes whose associated elements in
    old_list should be exclusive of the sorting in the rest of the list.
    :param old_list: The unsorted list, to-be sorted using the comparator and
    static_index_list.
    """

    sorted_list = []
    non_sort_subjects = list(map(lambda static_index: old_list[static_index], static_index_list))

    def sort_subject_filter(element):
        return element not in non_sort_subjects

    sort_subjects = sorted(list(filter(sort_subject_filter, old_list)))

    whole_index = sort_subject_index = 0
    while whole_index &lt; len(old_list):
        if whole_index in static_index_list:
            sorted_list.append(non_sort_subjects[whole_index - sort_subject_index])
        else:
            sorted_list.append(sort_subjects[sort_subject_index])
            sort_subject_index += 1
        whole_index += 1

    return sorted_list


Reviews on the algorithm itself in addition to formatting/code style are appreciated.
Limitations

The implementation will not work well for certain inputs with duplicate values. The spec doesn't say that there won't be duplicate values, so you should not assume that.

This limitation can be fixed by excluding items to sort by index instead of value.

Performance

element not in non_sort_subjects and whole_index in static_index_list will be linear searches, since non_sort_subjects and static_index_list are lists. A simple improvement would be to search in sets.