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,

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
words.sort()
unique = []
total_words = len(words)
i = 0

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

print(unique)

Output

This will give the output −

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements