
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to remove all duplicates word from a given sentence.
In this article, we are going to learn how to remove all the duplicate words from the given sentence.
Generally, we will encounter situations where the sentence contains the same word repeated multiple times. These duplicate words make the text look messy and can affect further processing.
Removing duplicates will help to improve the readability and ensure each word appears only once in the final sentence.
Using Python split() Method
The Python split() method is used to split all the words in the string using the specified separator. The separator can be a comma, full-stop, or any other character used to separate the string.
Syntax
Following is the syntax of the Python split() method -
str.split(separator, maxsplit)
In this approach, we are going to use the split() method to split the sentence into a list of words and initialize an empty list to add unique words and keep track of the words. Here, we are going to add the first occurrence of each word and remove any later.
Example
Let's look at the following example, where we are going to consider the sentence "Welcome Everyone Everyone Welcome" and remove the duplicates.
def demo(x): a = x.split() b = [] c = set() for word in a: if word not in c: b.append(word) c.add(word) return ' '.join(b) print(demo("Welcome Everyone Everyone Welcome"))
The output of the above program is as follows -
Welcome Everyone