Python – Replace duplicate Occurrence in String

Sometimes, while working with Python strings, we can have problem in which we need to perform the replace of a word. This is quite common task and has been discussed many times. But sometimes, the requirement is to replace occurrence of only duplicate, i.e from second occurrence. This has applications in many domains. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using split() + enumerate() + loop
The combination of above functions can be used to perform this task. In this, we separate the words using split. In this, we memoize the first occurrence in set and check if the value is saved before and then is replaced is already occurred.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Replace duplicate Occurrence in String
# Using split() + enumerate() + loop
  
# initializing string
test_str = 'Gfg is best . Gfg also has Classes now. \
                Classes help understand better . '
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing replace mapping 
repl_dict = {'Gfg' 'It', 'Classes' : 'They' }
  
# Replace duplicate Occurrence in String
# Using split() + enumerate() + loop
test_list = test_str.split(' ')
res = set()
for idx, ele in enumerate(test_list):
    if ele in repl_dict:
        if ele in res:
            test_list[idx] = repl_dict[ele]
        else:
            res.add(ele)
res = ' '.join(test_list)
  
# printing result 
print("The string after replacing : " + str(res)) 

chevron_right


Output :

The original string is : Gfg is best . Gfg also has Classes now. Classes help understand better .
The string after replacing : Gfg is best . It also has Classes now. They help understand better .

 



Method #2 : Using keys() + index() + list comprehension
This is yet another way in which this task can be performed. In this, we don’t require memoization. This is one liner approach to solve this problem.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Replace duplicate Occurrence in String
# Using keys() + index() + list comprehension
  
# initializing string
test_str = 'Gfg is best . Gfg also has Classes now. Classes help understand better . '
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing replace mapping 
repl_dict = {'Gfg' 'It', 'Classes' : 'They' }
  
# Replace duplicate Occurrence in String
# Using keys() + index() + list comprehension
test_list = test_str.split(' ')
res = ' '.join([repl_dict.get(val) if val in repl_dict.keys() and test_list.index(val) != idx 
                                   else val for idx, val in enumerate(test_list)])
  
# printing result 
print("The string after replacing : " + str(res)) 

chevron_right


Output :

The original string is : Gfg is best . Gfg also has Classes now. Classes help understand better .
The string after replacing : Gfg is best . It also has Classes now. They help understand better .




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.