How can I remove duplicate words in a string with Python?

string1 = "calvin klein design dress calvin klein"
words = string1.split()
print (" ".join(sorted(set(words), key=words.index)))

This sorts the set of all the (unique) words in your string by the word’s index in the original list of words.

Leave a Comment