Replace substring in list of strings – Python
We are given a list of strings, and our task is to replace a specific substring within each string with a new substring. This is useful when modifying text data in bulk. For example, given a = [“hello world”, “world of code”, “worldwide”], replacing “world” with “universe” should result in [“hello universe”, “universe of code”, “universewide”]. Let’s discuss different methods to do this in Python.
Using List Comprehension with replace()
replace() method replaces occurrences of a substring in a string. Using list comprehension, we can apply it to every string in the list efficiently.
[GFGTABS]
a = ["hello world", "world of code", "worldwide"]
old_substring = "world"
new_substring = "universe"
res = [s.replace(old_substring, new_substring) for s in a]
print(res)
[/GFGTABS]
['hello universe', 'universe of code', 'universewide']
Explanation:
- The replace() function replaces all occurrences of “world” with “universe” in each string.
- List comprehension iterates through the list and applies replace() to every string.
Using map()
map() function applies replace() to each string in the list without using explicit loops.
[GFGTABS]
a = ["hello world", "world of code", "worldwide"]
s1 = "world"
s2 = "universe"
res = list(map(lambda s: s.replace(s1, s2), a))
print(res)
[/GFGTABS]
['hello universe', 'universe of code', 'universewide']
Explanation:
- map() applies the replace() function to each string in a.
- The lambda function takes each string and replaces “world” with “universe”.
- list(map(…)) ensures that the result is stored as a list.
Using for Loop and replace()
A for loop allows modifying the list step by step, storing results in a new list.
[GFGTABS]
a = ["hello world", "world of code", "worldwide"]
s1 = "world"
s2 = "universe"
res = []
for s in a:
s3 = s.replace(s1, s2)
res.append(s3)
print(res)
[/GFGTABS]
['hello universe', 'universe of code', 'universewide']
Explanation:
- The for loop iterates over each string in a.
- s.replace(s1, s2) replaces “world” with “universe” in each string.
- The modified string is stored in result using append().
Using re.sub() for Pattern-Based Replacement
re.sub() function allows replacing substrings based on patterns.
[GFGTABS]
import re
a = ["hello world", "world of code", "worldwide"]
s1 = "world"
s2 = "universe"
res = [re.sub(s1, s2, s) for s in a]
print(res)
[/GFGTABS]
['hello universe', 'universe of code', 'universewide']
Explanation:
- re.sub(pattern, replacement, string) replaces “world” with “universe” in each string.
- This method is useful when replacing text based on complex patterns rather than exact substrings.
Using str.translate()
If replacing single characters instead of substrings, str.translate() is a faster alternative.
[GFGTABS]
a = ["hello world", "world of code", "worldwide"]
trans_table = str.maketrans("o", "0")
res = [s.translate(trans_table) for s in a]
print(res)
[/GFGTABS]
['hell0 w0rld', 'w0rld 0f c0de', 'w0rldwide']
Explanation:
- str.maketrans(“o”, “0”) creates a mapping to replace “o” with “0”.
- s.translate(trans_table) applies this mapping to each string in a.
- This method is limited to character replacements rather than full substrings.
Related Articles: