VOOZH about

URL: https://www.geeksforgeeks.org/python/python-replace-duplicate-occurrence-in-string/

⇱ Python - Replace duplicate Occurrence in String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Replace duplicate Occurrence in String

Last Updated : 7 Nov, 2025

In this article, we will discuss how to replace only the duplicate occurrences of certain words in a string that is, replace a word from its second occurrence onwards, while keeping its first occurrence unchanged.

Example:

Input: Gfg is best. Gfg also has Classes now. Classes help understand better.
Output: Gfg is best. It also has Classes now. They help understand better.

Using list comprehension + set

This is the cleanest and most efficient approach. We use a set to keep track of seen words and a list comprehension to perform replacements in a single line.

Steps:

  1. Split the string into individual words.
  2. Iterate over each word.
  3. If it exists in the replacement dictionary and hasn’t been seen yet, keep it as-is.
  4. If it appears again, replace it with the mapped value.
  5. Join the modified words back into a single string.

Output
Gfg is best . It also has Classes now. They help understand better .

Explanation:

  • split(): breaks the string into words.
  • set(): tracks words that appeared once.
  • (seen.add(word) or word): ensures first appearance is stored but not replaced
  • ' '.join(res): combines words back into a string.

Using Regular Expressions

This approach uses regex patterns to detect repeated occurrences of target words and replaces them using re.sub().

Steps:

  1. Import the re module.
  2. Define a regex pattern to match the target words from the replacement dictionary.
  3. Use a function in re.sub() to perform conditional replacements based on previous occurrences.

Output
Gfg is best . It also has Classes now. They help understand better .

Explanation:

  • \b(...)\b: matches complete words only.
  • re.escape(): safely handles special regex characters in keys.
  • re.sub(pattern, fun, s1): calls fun() for each match and replaces only later duplicates using a seen set.

Using split() + enumerate() + Loop

This is a more explicit and beginner-friendly approach using loops and sets. It manually iterates through the words, tracks first occurrences, and replaces duplicates.


Output
Gfg is best . It also has Classes now. They help understand better .

Explanation:

  • s1.split(): splits text into tokens.
  • if word in rep and word in seen: replaces repeat occurrences using mapping.
  • seen.add(word): records first encounter.

Using keys() + index() + List Comprehension

This method uses the list.index() function inside a list comprehension to find if a word has appeared before. It replaces only the repeated words and keeps the first one as it is.


Output
Gfg is best . It also has Classes now. They help understand better .

Explanation:

  • rep.get(word): retrieves replacement from dictionary.
  • words.index(word) != i: ensures only later occurrences are replaced.

 Related Articles:

Comment