![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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']
Advertisements