Open In App

Python | Remove all Duplicates Words from a given Sentence

Last Updated : 25 Oct, 2025
Comments
Improve
Suggest changes
14 Likes
Like
Report

Given a sentence, the task is to remove all duplicate words while keeping only the first occurrence of each word. For example:

Input: "Geeks for Geeks"
Output: "Geeks for"

Let's explore different methods to remove duplicate words efficiently in Python.

Using dict.fromkeys()

This method uses a dictionary, which automatically removes duplicates while maintaining the order of words.

Python
s1 = "Geeks for Geeks"
s2 = s1.split() 
s3 = list(dict.fromkeys(s2))
s4 = ' '.join(s3)
print(s4)

Output
Geeks for

Explanation:

  • s1.split() splits the sentence into words -> ['Geeks', 'for', 'Geeks'].
  • dict.fromkeys(s2) creates keys from words, automatically removing duplicates while maintaining their first occurrence.
  • list(dict.fromkeys(s2)) converts dictionary keys back to a list.
  • ' '.join(s3) joins list elements back into a string separated by spaces.

Using List Comprehension with Set

This method uses a set to track seen words and list comprehension to build the final list. It keeps the order and removes duplicates efficiently.

Python
s1 = "Geeks for Geeks"
a = s1.split() 
seen = set()  
res = [word for word in a if not (word in seen or seen.add(word))]
s2 = ' '.join(res)
print(s2)

Output
Geeks for

Explanation:

  • seen = set() empty set created to record words already seen.
  • The condition not (word in seen or seen.add(word)) adds a word to result only if it hasn’t appeared before.
  • ' '.join(res) joins words back to form the final sentence.

Using set with join()

A set in Python is a collection that automatically removes duplicate values. This method is the simplest and fastest. However, set does not maintain order hence, it's not ideal for situations where maintaining order is intended.

Python
s1 = "Geeks for Geeks"
s2 = s1.split()  
s3 = list(set(s2))
s4 = ' '.join(s3)
print(s4)

Output
Geeks for

Explanation:

  • s1.split() splits the sentence into words.
  • set(s2) removes duplicates automatically since sets can’t contain repeated elements.
  • Converting the set back to a list may change the order of words.

Using Simple Loop

This method is the most basic way of removing duplicates but it can be slower for longer sentences. It loops through each word checks if it's already been added to a result list and adds it only if it hasn't appeared before.

Python
s1 = "Geeks for Geeks"
s2 = s1.split()  
res = []  

for word in s2:
    if word not in res:
        res.append(word)
        
s3 = ' '.join(res)
print(s3)

Output
Geeks for

Explanation:

  • The loop checks each word in s2.
  • If word not already in res, it’s added to maintain first occurrence.
  • ' '.join(res) rebuilds the sentence from unique words.

Remove all duplicates words from a given sentence

Explore