remove duplicate of specific string from the list of string

remove duplicate of specific string from the list of string

Problem Description:

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?

Solution – 1

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)
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject