def _substitute_window(seq, idx_freq_win, order=2):
    """
    This function carries out substitution of the most frequent window with an
    integer. Each window substitution reduces the length of the sequence by
    (order-1) elements. The substituted integer is one greater than the largest
    element in the sequence.

    The substitution uses indices/locations of the most frequent window and
    is carried out in 3 steps, for a single window:
        1. Replace 1st element of seq at window index by integer (max(seq)+1)
        2. Replace the next order - 1 elements with False
        3. Filter list to eliminate all False elements
    Iteration of first 2 steps happens together over indices in idx_freq_win

    Parameters
    ----------
    seq : list
        Sequence of integers.
    idx_freq_win : compress iterator or tuple or list
        Indices that correspond to the occurence of the most frequent window.
        The index of the window is the same as the index of the first element
        of that window in seq.
    order : int, optional
        Number of elements in window for substitution.
        The default is 2 for pairs.

    Returns
    -------
    reduced_seq : list
        Sequence of integers with most frequent window substituted.
            len(reduced_seq) < len(seq)

    """
    # Integer value to use for substitution
    a = max(seq) + 1

    # Iterate over indices/locations of most frequent window
    for index in idx_freq_win:

        # Substitute the first element of window, in seq, by integer
        seq[index] = a

        # Substitute the remaining elements of window, in seq, with False
        for n in range(1, order):
            seq[index + n] = False

    # Filter out all False elements and return what's left
    reduced_seq = [element for element in seq if element]

    return reduced_seq