Replace substring in list of strings - Python
Last Updated :
22 Apr, 2025
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.
Python
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)
Output['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.
Python
a = ["hello world", "world of code", "worldwide"]
s1 = "world"
s2 = "universe"
res = list(map(lambda s: s.replace(s1, s2), a))
print(res)
Output['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.
Python
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)
Output['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.
Python
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)
Output['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.
Python
a = ["hello world", "world of code", "worldwide"]
trans_table = str.maketrans("o", "0")
res = [s.translate(trans_table) for s in a]
print(res)
Output['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:
Similar Reads
Replace Substrings from String List - Python The task of replacing substrings in a list of strings involves iterating through each string and substituting specific words with their corresponding replacements. For example, given a list a = ['GeeksforGeeks', 'And', 'Computer Science'] and replacements b = [['Geeks', 'Gks'], ['And', '&'], ['C
3 min read
Python | Substring removal in String list While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which
5 min read
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Tokenizing Strings in List of Strings - Python The task of tokenizing strings in a list of strings in Python involves splitting each string into smaller units, known as tokens, based on specific delimiters. For example, given the list a = ['Geeks for Geeks', 'is', 'best computer science portal'], the goal is to break each string into individual
2 min read
List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read