Python - Replace duplicate Occurrence in String
Last Updated : 08 May, 2023
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))
OutputThe 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))
OutputThe 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 Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read