Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Replace substring in list of strings

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 11 Jan, 2023
Improve Article
Save Article

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

Output : 

The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks']
The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']

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

Output : 

The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks']
The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']

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

Output

The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks']
The list after substring replacement : ['1', 'kg', 'butter', 'for', '10', 'bucks']

This method has a time complexity of O(n) and an auxiliary space of O(n), where n is the length of test_list.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!