remove duplicate of specific string from the list of string

Question:

I want to remove specific string from the list of string. Suppose I have a list like this:

list_ex = ['I', 'went', 'to', 'the', 'big', 'conference', ',', 'I', 'presented', 'myself', 'there', '.', 'After', 'the', '<word>conference</word>', '<word>conference</word>', ',', 'I', 'took', 'a', 'taxi', 'to', 'go', 'to', 'the', '<word>hotel</word>', '<word>hotel</word>', '.']

I want to remove duplicate string for example: "<"word>keyword</word">".

My desired output:

new_list_ex = ['I', 'went', 'to', 'the', 'big', 'conference', ',', 'I', 'presented', 'myself', 'there', '.', 'After', 'the', '<word>conference</word>', ',', 'I', 'took', 'a', 'taxi', 'to', 'go', 'to', 'the', '<word>hotel</word>', '.']

I know how to remove duplicate items from the list but how to remove specific elements in this case by reserving the order?

Asked By: Erwin

||

Answers:

That is a simple for loop with the appropriate if condition:

new_list_ex = []
for item in list_ex:
    if item.startswith('<word>') and item in new_list_ex:
        continue
    new_list_ex.append(item)
Answered By: 9769953
new_list_ex = []
for item in list_ex:
    if item.startswith('<word>') and item in new_list_ex:
        continue
    new_list_ex.append(item)
Answered By: Ujjwal Jha
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.