Home > database >  remove duplicate of specific string from the list of string
remove duplicate of specific string from the list of string

Time:12-06

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?

CodePudding user response:

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)

CodePudding user response:

new_list_ex = []
for item in list_ex:
    if item.startswith('<word>') and item in new_list_ex:
        continue
    new_list_ex.append(item)
  • Related