Here is the original list: Liste = ['hello','hello world','nice world','nice'] and I want to remove the duplicate words in that list and save the non duplicate words in a list. so the result List will be like this: ['hello', world', 'nice'] So anyone can help me? Thank you in advanced.
Asked
Active
Viewed 81 times
-4
-
Do you want to preserve the order of the result? If not, you can use `split` and `set` (or set comprehension); `output = {word for phrase in lst for word in phrase.split()}` – j1-lee Jun 28 '22 at 19:51
2 Answers
3
list({ww: 1 for w in [f.split(' ') for f in foo] for ww in w})
relies on dicts preserving the insertion order.

Shabble
- 542
- 3
- 10
-
@fsimonjetz good point. I'd argue it's clearer with it there, but that's at odds with the rest of my answer :-) – Shabble Jun 28 '22 at 20:00
1
For this answer I didn't use list comprehensions as it would be too hard for the author to understand, judging the difficulty of the question, it's safe to assume that he's fairly new.
duped_list = ['hello','hello world','nice world','nice']
def cleanse(duped_list):
cleansed = []
for words in duped_list:
for word in words.split():
if word not in cleansed:
cleansed.append(word)
return cleansed

Tirterra
- 579
- 2
- 4
- 14