Open In App

Python – Replace duplicate Occurrence in String

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Python3




# 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))


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 .

Time Complexity: O(n)

Space Complexity: O(n)

  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. 

Python3




# 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))


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 .

Time Complexity: O(n)

Space Complexity: O(n)

Method #3 : Using  list comprehension + set + keys This is yet another way in which this task can be performed.

  • The code initializes a string variable named test_str with the value ‘Gfg is best . Gfg also has Classes now. Classes help understand better . ‘.
  • The code prints the original string using the print() function and string concatenation.
  • The code initializes a dictionary variable named repl_dict with key-value pairs, where each key is a string that needs to be replaced, and its corresponding value is the string to replace it with.
  • The code splits the test_str string into a list of words using the split() method, and initializes an empty set named seen to keep track of which words have been replaced.
  • The code uses a list comprehension to replace duplicate occurrences of words in the list of words generated in the previous step. The list comprehension iterates over each word in the list and checks if it is present in the repl_dict dictionary and also not present in the seen set. If the word is present in the dictionary and not in the seen set, it is replaced with its corresponding value from the dictionary, and the word is added to the seen set. Otherwise, the original word is used.
  • The code joins the list of words generated in the previous step using the join() method with a space separator to form the final string, and assigns the result to the res variable.
  • The code prints the final string using the print() function and string concatenation.

Python3




# 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 set() + loop + list comprehension
words = test_str.split()
seen = set()
res = [repl_dict[word] if word in repl_dict and word not in seen and not seen.add(word) else word for word in words]
 
# join the list of words to form the final string
res = ' '.join(res)
 
# printing result
print("The string after replacing : " + str(res))


Output

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

Time complexity: O(n), where n is the length of the input string. This is because we iterate over each word in the string exactly once.

Space complexity: O(n), where k is the number of keys in the replacement dictionary. This is because we use a set to store the words that have already been replaced.

Method #4: Using regular expressions

This method uses the re module to search for the repeated occurrences of the words in the string and replaces them with their corresponding values from the repl_dict.

step-by-step approach:

Import the re module.
Initialize the test_str and repl_dict variables.
Define a regular expression pattern to match repeated occurrences of the words in the repl_dict.
Use the re.sub() method to replace the matched patterns with their corresponding values from the repl_dict.
Print the original and replaced strings.

Python3




import re
 
# initializing string
test_str = 'Gfg is best . Gfg also has Classes now. \
                Classes help understand better . '
 
# initializing replace mapping
repl_dict = {'Gfg': 'It', 'Classes': 'They'}
 
# regular expression pattern to match repeated occurrences
pattern = r'\b(' + '|'.join(repl_dict.keys()) + r')\b(?=.*\b\1\b)'
 
# Replace duplicate Occurrence in String
res = re.sub(pattern, lambda match: repl_dict[match.group()], test_str)
 
# printing result
print("The original string is : " + str(test_str))
print("The string after replacing : " + str(res))


Output

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

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1)



Similar Reads

Python | Replace multiple occurrence of character by single
Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character. Examples: Input : Geeksforgeeks, ch = 'e' Output : Geksforgeks Input : Wiiiin, ch = 'i' Output : WinReplace multiple occurrence of character by singleApproach #1 : Naive Approach This method is a brute force approach
4 min read
replace() in Python to replace a substring
Given a string str that may contain one more occurrences of “AB”. Replace all occurrences of “AB” with “C” in str. Examples: Input : str = "helloABworld" Output : str = "helloCworld" Input : str = "fghABsdfABysu" Output : str = "fghCsdfCysu" This problem has existing solution please refer Replace all occurrences of string AB with C without using ex
1 min read
Python | Pandas Series.str.replace() to replace text in a series
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Panda
5 min read
Python - Extract String till all occurrence of characters from other string
Given a string, the task is to write a Python program to extract till all characters from other strings are found. Input : test_str = "geeksforgeeks is best for all geeks", check_str = "freak"Output : geeksforgeeks is best for aExplanation : a is last letter in freak to be present in string. The string is printed till first occurrence of a. Input :
11 min read
Generate two output strings depending upon occurrence of character in input string in Python
Given an input string str[], generate two output strings. One of which consists of that character that occurs only once in the input string and the second consists of multi-time occurring characters. Output strings must be sorted. Examples: Input : str = "geeksforgeeks" Output : String with characters occurring once: "for". String with characters o
4 min read
Python program to find occurrence to each character in given string
Given a string, the task is to write a program in Python that prints the number of occurrences of each character in a string. There are multiple ways in Python, we can do this task. Let's discuss a few of them. Method #1: Using set() + count() Iterate over the set converted string and get the count of each character in original string. C/C++ Code #
5 min read
Python | Get the string after occurrence of given substring
Sometimes, more than finding a substring, we might need to get the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python. Using partition() to get string after occurrence of given substring The partition function can be used to perform this task in which we just retu
3 min read
Python | Ways to find nth occurrence of substring in a string
Given a string and a substring, write a Python program to find the nth occurrence of the string. Let's discuss a few methods to solve the given task. Get Nth occurrence of a substring in a String using regex Here, we find the index of the 'ab' character in the 4th position using the regex re.finditer() C/C++ Code import re # Initialising values ini
4 min read
Python | Split string on Kth Occurrence of Character
Many problems of the split have been dealt with in the past, but sometimes, one can also encounter this problem in which we need to split the string on the Kth occurrence of a character. Let's discuss certain ways in which this problem can be solved. Method #1 : Using split() + join() The split and join method can perform this particular task. In t
6 min read
Python | First character occurrence from rear String
There are many ways to find out the first index of element in String as python in its language provides index() function that returns the index of first occurrence of element in String. But if one desires to get the last occurrence of element in string, usually a longer method has to be applied. Lets discuss certain shorthands to achieve this parti
4 min read
Practice Tags :
three90RightbarBannerImg