How toremove duplicate words in a string with Python?
Published on Aug. 22, 2023, 12:14 p.m.
There are multiple ways to remove duplicate words from strings in a list in Python, as highlighted by the search results. Here are a few methods you could use:
Method 1: Using a loop and a set
original_list = ['hello world', 'goodbye world', 'hello universe']
new_list = []
for phrase in original_list:
words = phrase.split()
unique_words = set(words)
new_phrase = ' '.join(unique_words)
new_list.append(new_phrase)
print(new_list)
Output:
['world hello', 'world goodbye', 'universe hello']
Method 2: Using a list comprehension and a set
original_list = ['hello world', 'goodbye world', 'hello universe']
new_list = [' '.join(set(phrase.split())) for phrase in original_list]
print(new_list)
Output:
['world hello', 'world goodbye', 'universe hello']
Method 3: Using the dict.fromkeys() method
original_list = ['hello world', 'goodbye world', 'hello universe']
new_list = []
for phrase in original_list:
words = phrase.split()
no_duplicates = list(dict.fromkeys(words))
new_phrase = ' '.join(no_duplicates)
new_list.append(new_phrase)
print(new_list)
Output:
['hello world', 'goodbye world', 'hello universe']
These are just a few of the many ways to remove duplicate words from strings in a list in Python. The best method to use will depend on your specific use case and preferences.