Remove all duplicates from a given string in Python

To remove all duplicates from a string in python, we need to first split the string by spaces so that we have each word in an array. Then there are multiple ways to remove duplicates.

We can remove duplicates by first converting all words to lowercase, then sorting them and finally picking only the unique ones. For example,


sent = "Hi my name is John Doe John Doe is my name"

# Seperate out each word
words = sent.split(" ")

# Convert all words to lowercase
words = map(lambda x:x.lower(), words)

# Sort the words in order
unique = []
total_words = len(words)
i = 0

while i < (total_words - 1):
while i < total_words and words[i] == words[i + 1]:
i += 1
i += 1



This will give the output −

['doe', 'hi', 'john', 'is', 'my']
Samual Sam
Samual Sam

Learning faster. Every day.