Python Replace duplicate Occurrence in String (original) (raw)

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. Python `

s1 = 'Gfg is best . Gfg also has Classes now. Classes help understand better .' rep = {'Gfg': 'It', 'Classes': 'They'} seen = set() res = [ rep[word] if word in rep and word in seen else (seen.add(word) or word) for word in t1.split() ] s2 = ' '.join(res) print( s2)

`

Output

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

**Explanation:

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. Python `

import re

s1 = 'Gfg is best . Gfg also has Classes now. Classes help understand better .' d = {'Gfg': 'It', 'Classes': 'They'} pattern = r'\b(' + '|'.join(re.escape(k) for k in d.keys()) + r')\b' seen = set()

def fun(m): word = m.group(1) if word in seen: return d[word]
seen.add(word) return word

res = re.sub(pattern, fun, s1) print(res)

`

Output

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

**Explanation:

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.

Python `

s1 = 'Gfg is best . Gfg also has Classes now. Classes help understand better .' rep = {'Gfg': 'It', 'Classes': 'They'} words = s1.split() seen = set() for i, word in enumerate(words): if word in rep: if word in seen: words[i] = rep[word] else: seen.add(word) res = ' '.join(words) print(res)

`

Output

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

**Explanation:

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.

Python `

s1 = 'Gfg is best . Gfg also has Classes now. Classes help understand better .' d = {'Gfg': 'It', 'Classes': 'They'} words = s1.split() res = ' '.join([ d.get(word) if word in d and words.index(word) != i else word for i, word in enumerate(words) ]) print(res)

`