Remove substring list from String - Python
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 removing all specified substrings efficiently.
Using String Replace in a Loop
This method iterates through the list of substrings and removes each from the string using the replace method.
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
for sub in a:
s = s.replace(sub, "")
print(s)
Output
for is an website.
Explanation:
- The replace() method is used to remove each substring by replacing it with an empty string.
- The loop ensures all substrings in sublist 'a' are processed.
Using Regular Expressions
Regular expressions provide a powerful way to remove multiple substrings in one step.
import re
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
pattern = "|".join(map(re.escape, a))
s = re.sub(pattern, "", s)
print(s)
Output
for is an website.
Explanation:
- re.escape() ensures special characters in substrings are handled safely.
- join() method creates a single pattern matching all substrings.
- re.sub() method replaces all occurrences of the pattern with an empty string.
Using List Comprehension and Join
This method builds a filtered string by excluding unwanted substrings.
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
res = " ".join(word for word in s.split() if all(sub not in word for sub in a))
print(res)
Output
is an website.
Explanation: This code removes words containing any substring from list "a" in the string "str". It splits the string into words, then keeps only those words that do not contain any substring from "a", and finally joins them back into a string.
Using Reduce from functools
reduce() function allows for successive application of the replace method.
from functools import reduce
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
res = reduce(lambda acc, sub: acc.replace(sub, ""), a, s)
print(res)
Output
for is an website.
Explanation: This code removes all substrings in list "a" from the string s using reduce. It repeatedly applies str.replace() for each substring in "a", replacing them with an empty string. The final result is the original string with all occurrences of "Geeks" and "awesome" removed.
Using a Custom Function with Loop
A custom function provides flexibility to handle edge cases or additional processing.
def remove_substrings(s, a):
for sub in a:
s = s.replace(sub, "")
return s
s = "GeeksforGeeks is an awesome website."
a = ["Geeks", "awesome"]
res = remove_substrings(s, a)
print(res)
Output
for is an website.
Explanation: remove_substrings iterates through each substring in list a and removes all its occurrences from the given string s using replace(). It returns the cleaned string after all specified substrings have been removed. In this example, it removes "Geeks" and "awesome" from the original string.
Related Articles: