Erwin
Erwin

Reputation: 381

remove duplicate of specific string from the list of string

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?

Upvotes: 0

Views: 56

Answers (1)

9769953
9769953

Reputation: 12192

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)

Upvotes: 2

Related Questions