While working with strings, one of the most used application is replacing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the replacement of a substring in list of string is performed. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension + replace() The replace method can be coupled with the list comprehension technique to achieve this particular task. List comprehension performs the task of iterating through the list and replace method replaces the section of substring with another.
Python3
# Python3 code to demonstrate # Replace substring in list of strings # using list comprehension + replace() # initializing list test_list = [ '4' , 'kg' , 'butter' , 'for' , '40' , 'bucks' ] # printing original list print ("The original list : " + str (test_list )) # using list comprehension + replace() # Replace substring in list of strings res = [sub.replace( '4' , '1' ) for sub in test_list] # print result print ("The list after substring replacement : " + str (res)) |
The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks'] The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']
Time complexity: O(n)
Auxiliary space: O(n), where n is length of list.
Method #2 : Using map() + lambda + replace() The combination of these functions can also be used to perform this particular task. The map and lambda help to perform the task same as list comprehension and replace method is used to perform the replace functionality. But this method is poor when it comes to performance than method above.
Python3
# Python3 code to demonstrate # Replace substring in list of strings # using list comprehension + map() + lambda # initializing list test_list = [ '4' , 'kg' , 'butter' , 'for' , '40' , 'bucks' ] # printing original list print ("The original list : " + str (test_list )) # using list comprehension + map() + lambda # Replace substring in list of strings res = list ( map ( lambda st: str .replace(st, " 4 ", " 1 "), test_list)) # print result print ("The list after substring replacement : " + str (res)) |
The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks'] The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #3 : Using Join()+ replace()
Here is a method that converts the list to a string, performs the replacement on the string, and then converts the modified string back to a list:
Python3
# Python3 code to demonstrate # Replace substring in list of strings # by converting list to string and back # initializing list test_list = [ '4' , 'kg' , 'butter' , 'for' , '40' , 'bucks' ] # printing original list print ( "The original list : " + str (test_list )) # convert list to string test_string = " " .join(test_list) # replace substring in string modified_string = test_string.replace( "4" , "1" ) # convert modified string back to list modified_list = modified_string.split() # print result print ( "The list after substring replacement : " + str (modified_list)) #This code is contributed by Edula Vinay Kumar Reddy |
The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks'] The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']
Time complexity: O(n)
Auxiliary space: O(n), where n is the length of test_list.
Method#4 : Using Re Module
step-by-step algorithm for implementing the approach
- Import the re module.
- Define the original list of strings.
- Define a regular expression pattern that matches the substring to be replaced.
- Use the compile() method of the re module to compile the pattern.
- Use a list comprehension to apply the sub() method of the pattern object to each element in the original list of strings, replacing all occurrences of the pattern with the new substring.
- Assign the resulting list to a new variable.
- Print the resulting list.
Python3
import re # initializing list test_list = [ '4' , 'kg' , 'butter' , 'for' , '40' , 'bucks' ] # replace substring using re module pattern = re. compile (r '4' ) res = [pattern.sub( '1' , sub) for sub in test_list] # print result print ( "The list after substring replacement : " + str (res)) |
The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']
Time Complexity:
- Compiling the regular expression pattern using the compile() method takes O(1) time.
- The sub() method is applied to each element in the original list of strings using a list comprehension, which takes O(n) time, where n is the length of the list.
- The time complexity of the sub() method itself depends on the size and complexity of the regular expression pattern and the length of the input string. In this case, the pattern is a simple substring and the input strings are relatively short, so we can assume that the sub() method takes O(1) time.
- Therefore, the overall time complexity of the algorithm is O(n).
Auxiliary Space Complexity:
- The algorithm uses O(n) auxiliary space to store the resulting list of strings.
- The re module may also use additional memory to store the compiled pattern and intermediate results, depending on the size and complexity of the pattern and the input strings.
- Therefore, the overall auxiliary space complexity of the algorithm is O(n) or higher, depending on the specific implementation details of the re module.
Method 5 : Using the itertools module.
step by step approach:
Import the itertools module.
Use the chain() function from itertools to join the original list of strings into a single string.
Use the replace() method to replace the substring in the single string.
Use the groupby() function from itertools to split the modified single string back into a list of strings based on spaces.
Use a list comprehension to convert the groupby object into a list of strings.
Print the modified list.
Python3
# Python3 code to demonstrate # Replace substring in list of strings # using itertools # importing itertools import itertools # initializing list test_list = [ '4' , 'kg' , 'butter' , 'for' , '40' , 'bucks' ] # joining list of strings into a single string joined_string = "".join(test_list) # replacing substring in the single string modified_string = joined_string.replace( "4" , "1" ) # splitting modified single string into list of strings using groupby() function grouped_strings = [" ".join(g) for k, g in itertools.groupby(modified_string, lambda x: x == " ") if not k] # printing modified list print ( "The list after substring replacement : " + str (grouped_strings)) |
The list after substring replacement : ['1kgbutterfor10bucks']
Time complexity: O(n)
Auxiliary space: O(n)