Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 Output
This will give the output −
['doe', 'hi', 'john', 'is', 'my']
Advertisements
