0

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?

2
  • 2
    What you do mean by "with phrase tag."? Commented Dec 6, 2022 at 12:33
  • sorry edited the question. Commented Dec 6, 2022 at 12:38

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.