Open In App

Python – Replace delimiter

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Initialize a list test_list with elements containing strings separated by commas.
  • Print the original list test_list.
  • Initialize a replacement delimiter repl_delim.
  • Create an empty list res.
  • Loop through each element ele in test_list.
  • Replace the comma delimiter in the string ele with the replacement delimiter repl_delim using the replace() method.
  • Append the modified string to the res list.
  • Print the modified list res.

Below is the implementation of the above approach:

Python3




# 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




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

  • Define a list of strings test_list and a replacement delimiter repl_delim.
  • Use list comprehension and iterate over each element of the test_list.
  • For each element, use the re.sub() method to replace the comma and space separator with the replacement delimiter.
  • Return the modified list as a result.

Python3




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:

  • Create an empty list res to store the modified strings.
  • Loop through each string in the test_list.
  • Split the string using split() method with delimiter , (comma followed by space).
  • Join the split strings using join() method with delimiter #.
  • Append the modified string to res.
  • Return the modified list res.

Below is the implementation of the above approach:

Python3




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

Python3




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

  • Initialize an empty list to store the modified strings.
  • Loop through each string in the original list of strings.
    • Within the loop, replace the delimiter (‘, ‘) with the desired replacement delimiter using string concatenation and the replace() method.
    • Append the modified string to the list of modified strings.
  • Return the modified list of strings.

Below is the implementation of the above approach:

Python3




# 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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads