Replace String in List Python
As we know Python string is immutable, means we can't change its value. In this case skill of replacing string in list comes handy.
In this article we will learn multiple different ways to replace string or substring in a list in Python.
- Using For Loop + replace()
- Using List Comprehension + replace()
- Using map() + lambda + replace()
- Using join() + replace()
- Using regex
- Speed Comparison
Table of Contents
1. Using For Loop + replace()
The replace() function is used to replace a substring with another substring in a string. We can use this function to replace a string in a list.
For this you need to iterate over the list using a for loop and replace the string using replace() function.
# list of strings
words = ['to', 'be[br]', 'or', 'not[br]', 'to', 'be']
# iterate over the list using for loop
for i in range(len(words)):
# replace the string
words[i] = words[i].replace('[br]', '<br>')
print(words)
Output:
['to', 'be<br>', 'or', 'not<br>', 'to', 'be']
Here we have used replace() function to replace [br] with <br> in the list.
Notice that replace() function returns a new string, so we have to assign it back to the list.
2. Using List Comprehension + replace()
List comprehension is another way you can use to replace string in a list.
Here is how you can do it.
# list of strings
words = ['to', 'be[br]', 'or', 'not[br]', 'to', 'be']
# replace the string using list comprehension
words = [word.replace('[br]', '<br>') for word in words]
print(words)
Output:
['to', 'be<br>', 'or', 'not<br>', 'to', 'be']
3. Using map() + lambda + replace()
The map() function apply lamda function to each element of the list. We can use this function to replace string in a list.
# list of strings
words = ['to', 'be[br]', 'or', 'not[br]', 'to', 'be']
# replace the string using map() + lambda
words = list(map(lambda word: word.replace('[br]', '<br>'), words))
print(words)
Output:
['to', 'be<br>', 'or', 'not<br>', 'to', 'be']
4. Using join() + replace()
Since we have a list of strings, we can join them using join() function which returns a string, and then replace the string using replace() function.
While joining the strings make sure to use a separator which is not present in the string, so that later we can convert it back to list using the same separator.
# list of strings
words = ['to', 'be[br]', 'or', 'not[br]', 'to', 'be']
# join the strings using join() function
# using 2 underscores as separator __
words = '__'.join(words)
# replace the string
words = words.replace('[br]', '<br>')
# convert back to list using the same separator
words = words.split('__')
print(words)
Output:
['to', 'be<br>', 'or', 'not<br>', 'to', 'be']
5. Using regex
Regular expression is known for its powerful string manipulation. We can use re.sub() function to replace string in a list.
import re
# list of strings
words = ['to', 'be[br]', 'or', 'not[br]', 'to', 'be']
# replace the string using re.sub()
words = [re.sub(r'\[br\]', '<br>', word) for word in words]
print(words)
Output:
['to', 'be<br>', 'or', 'not<br>', 'to', 'be']
Speed Comparison
Speed matters a lot when it comes to programming. So using the fastest method is always a good idea.
We run all the above methods 100000 times, measured the time taken by each method and plotted following graph.
You can clearly see that Method 4 is the fastest method to replace string in a list, followed by Method 2, Method 1, Method 3 and Method 5.