Open In App

Python – Replace delimiter

Given List of Strings and replacing delimiter, replace current delimiter in each string.

Input : test_list = [“a, t”, “g, f, g”, “w, e”, “d, o”], repl_delim = ‘ ‘ 
Output : [“a t”, “g f g”, “w e”, “d o”] 
Explanation : comma is replaced by empty spaces at each string.



 Input : test_list = [“g#f#g”], repl_delim = ‘, ‘ 
Output : [“g, f, g”] 
Explanation : hash is replaced by comma at each string.

Method #1 : Using replace() + loop The combination of above functions provide a brute force method to solve this problem. In this, a loop is used to iterate through each string and perform replacement using replace(). 



Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Replace delimiter
# Using loop + replace()
 
# initializing list
test_list = ["a, t", "g, f, g", "w, e", "d, o"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing replace delimiter
repl_delim = '#'
 
# Replace delimiter
res = []
for ele in test_list:
      
    # adding each string after replacement using replace()
    res.append(ele.replace(", ", repl_delim))
 
# printing result
print("Replaced List : " + str(res))

Output : 
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

Time Complexity: O(n), because list size increases with every new appended string. 
Auxiliary Space: O(n), since we are creating a new list of size n. 

  Method #2 : Using list comprehension + replace() The combination of above functions can provide one liner to this problem. This is similar to above method, just encapsulated in list comprehension. 




# Python3 code to demonstrate working of
# Replace delimiter
# Using list comprehension + replace()
 
# initializing list
test_list = ["a, t", "g, f, g", "w, e", "d, o"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing replace delimiter
repl_delim = '#'
 
# Replace delimiter
# iterating inside comprehension, performing replace using replace()
res = [sub.replace(', ', repl_delim) for sub in test_list]
 
# printing result
print("Replaced List : " + str(res))

Output : 
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  list comprehension and replace() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

  Method #3 : Using re.sub():

Algorithm:




import re
 
test_list = ["a, t", "g, f, g", "w, e", "d, o"]
repl_delim = '#'
# printing original list
print("The original list is : " + str(test_list))
  
 
res = [re.sub(', ', repl_delim, sub) for sub in test_list]
  
# printing result
print("Replaced List : " + str(res))
#This code is contributed by Jyothi pinjala

Output
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

Time Complexity:
The time complexity of the code is O(n*m), where n is the length of the input list and m is the average length of each string in the list. This is because we are iterating over each string in the list and performing a regular expression operation on it.

Auxiliary Space:
The auxiliary space of the code is O(n*m), where n is the length of the input list and m is the average length of each string in the list. This is because we are creating a new list of the same size as the input list, and each string in the new list may be longer than the original due to the replacement of the separator with the replacement delimiter. Additionally, the re.sub() method may create additional memory usage while performing the operation.

Method #4: Using the join() method and split function

Step-by-step approach:

Below is the implementation of the above approach:




# original list of strings
test_list = ["a, t", "g, f, g", "w, e", "d, o"]
 
# delimiter to replace
repl_delim = '#'
 
# printing original list
print("The original list is : " + str(test_list))
 
# new list to store modified strings
res = []
 
# loop through each string in test_list
for sub in test_list:
    # split the string using delimiter ', ' (comma followed by space)
    split_str = sub.split(', ')
    # join the split strings using delimiter '#'
    modified_str = repl_delim.join(split_str)
    # append the modified string to res
    res.append(modified_str)
 
# printing result
print("Replaced List : " + str(res))

Output
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

Time complexity: O(n), where n is the number of strings in the list. 
Auxiliary space: O(n), where n is the number of strings in the list.

Method #5: Using map() function and lambda expression

We can use the map() function with a lambda expression to replace the delimiter in each string of the list. Here is the step-by-step approach:

  1. Define the original list of strings and the delimiter to replace.
  2. Use the map() function with a lambda expression to replace the delimiter in each string.
  3. Convert the map object to a list to get the final result.




# original list of strings
test_list = ["a, t", "g, f, g", "w, e", "d, o"]
 
# delimiter to replace
repl_delim = '#'
 
# printing original list
print("The original list is : " + str(test_list))
 
# using map() and lambda expression to replace delimiter
res = list(map(lambda s: s.replace(', ', repl_delim), test_list))
 
# printing result
print("Replaced List : " + str(res))

Output
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

Time Complexity: O(n), where n is the number of strings in the list. The map() function and the replace() method both have a time complexity of O(n), and we are calling them once for each string in the list.
Auxiliary Space: O(n), where n is the number of strings in the list. We are creating a new list to store the modified strings, which has the same size as the original list.

Method #6: Using regular for loop and string concatenation

Step-by-step approach:

Below is the implementation of the above approach:




# original list of strings
test_list = ["a, t", "g, f, g", "w, e", "d, o"]
 
# delimiter to replace
repl_delim = '#'
 
# printing original list
print("The original list is : " + str(test_list))
 
# using regular for loop and string concatenation to replace delimiter
res = []
for s in test_list:
    modified_s = s.replace(', ', repl_delim)
    res.append(modified_s)
 
# printing result
print("Replaced List : " + str(res))

Output
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

Time Complexity: O(n * m), where n is the number of strings in the list and m is the length of the longest string in the list.
Auxiliary Space: O(n), where n is the number of strings in the li


Article Tags :