def dedupe_and_sort(sequence, first=None, last=None):
    first = first or []
    last = last or []
    new_sequence = [i for i in first if i in sequence]
    for item in sequence:
        if item not in new_sequence and item not in last:
            new_sequence.append(item)
    new_sequence.extend([i for i in last if i in sequence])
    return type(sequence)(new_sequence)