'removing all duplicates except one [duplicate]

I'm trying to remove all the duplicate words except for two certain words.

def reduceDuplicates(words, wordsIter):
    theWords=set()

    for word in words:

        if word!=FIRST or word!=LAST: #FIRST="first" #LAST="last"
            if word not in theWords:
                theWords.add(word)      #create new txt file 
        
        else:
            break

    words.__init__(theWords)     #call constructor to print

Does anybody know where to go with this? I've tried everything I can think of. The words "first" and "last" in my txt file keep losing their duplicates. I'm trying to keep those duplicates.

wordsIter is a list iterator that is supposed to handle mutator and accessor methods on the list of words variable



Solution 1:[1]

It is a bit unclear what words and wordsIter are in your code example. This function should do the trick though:

from typing import List, Set


def remove_duplicates(words: List[str], words_with_duplicates: Set[str]) -> List[str]:
    """
    Return the list of words in the same order, with all duplicates except those from `words_with_duplicates` removed
    """

    seen = set()
    return [x for x in words if x in words_with_duplicates or not (x in seen or seen.add(x))]


output = remove_duplicates(words=['A', 'B', 'C', 'B', 'D', 'D', 'C', 'B', 'D', 'B', 'A'],
                           words_with_duplicates={'B', 'D'})
assert output == ['A', 'B', 'C', 'B', 'D', 'D', 'B', 'D', 'B']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1