def _apply_order_patch(patch, group_list):
    """patch is a map from a key sequence to list of sequence, name pairs, and
    group_list is an ordered list of sequence, group, subgroup, name tuples.
    Iterate through the group list appending each item to a new list, and
    after appending an item matching a key sequence, also append all of its
    associated sequences in order using the same group and subgroup.
    Return the new list.  If there are any unused patches, raise an exception."""

    result = []
    patched = set()
    for t in group_list:
        result.append(t)
        if t[0] in patch:
            patched.add(t[0])
            _, group, subgroup, _ = t
            for seq, name in patch[t[0]]:
                result.append((seq, group, subgroup, name))

    unused = set(patch.keys()) - patched
    if unused:
        raise Exception(
            "%d unused patch%s\n  %s: "
            % (
                len(unused),
                "" if len(unused) == 1 else "es",
                "\n  ".join(seq_to_string(seq) for seq in sorted(unused)),
            )
        )

    return result