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:
- Split the string into individual words.
- Iterate over each word.
- If it exists in the replacement dictionary and hasn’t been seen yet, keep it as-is.
- If it appears again, replace it with the mapped value.
- Join the modified words back into a single string.
Pythons1='Gfg is best . Gfg also has Classes now. Classes help understand better .'rep={'Gfg':'It','Classes':'They'}seen=set()res=[rep[word]ifwordinrepandwordinseenelse(seen.add(word)orword)forwordint1.split()]s2=' '.join(res)print(s2)OutputGfg 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 usingre.sub().
Steps:
- Import the re module.
- Define a regex pattern to match the target words from the replacement dictionary.
- Use a function in re.sub() to perform conditional replacements based on previous occurrences.
Pythonimportres1='Gfg is best . Gfg also has Classes now. Classes help understand better .'d={'Gfg':'It','Classes':'They'}pattern=r'\b('+'|'.join(re.escape(k)forkind.keys())+r')\b'seen=set()deffun(m):word=m.group(1)ifwordinseen:returnd[word]seen.add(word)returnwordres=re.sub(pattern,fun,s1)print(res)OutputGfg 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.
Pythons1='Gfg is best . Gfg also has Classes now. Classes help understand better .'rep={'Gfg':'It','Classes':'They'}words=s1.split()seen=set()fori,wordinenumerate(words):ifwordinrep:ifwordinseen:words[i]=rep[word]else:seen.add(word)res=' '.join(words)print(res)OutputGfg 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.
Pythons1='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)ifwordindandwords.index(word)!=ielsewordfori,wordinenumerate(words)])print(res)OutputGfg 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:
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice