Remove Substring from Strings in Python

How can I remove substring from string python?

I have a set of strings, and all the strings contain one of two specific substrings (“.good” or “.bad”) that I want to remove:

set1 = {‘Apple.good’, ‘Orange.good’, ‘Pear.bad’, ‘Pear.good’, ‘Banana.bad’, ‘Potato.bad’}

I want to remove the “.good” and “.bad” substrings from all the strings. I tried this:

for x in set1: x.replace(‘.good’, ‘’) x.replace(‘.bad’, ‘’)

But it doesn’t seem to work, and set1 stays exactly the same. I also tried using for x in list(set1), but that doesn’t change anything either. How can I achieve this?

Ah, I see the problem here! In Python, the replace() method doesn’t modify the string in place—it returns a new string because strings are immutable. Here’s a quick and efficient way to fix this using set comprehension:

set1 = {'Apple.good', 'Orange.good', 'Pear.bad', 'Pear.good', 'Banana.bad', 'Potato.bad'}

# Use a set comprehension to remove the substrings
set1 = {x.replace('.good', '').replace('.bad', '') for x in set1}

print(set1)

This one-liner processes each element of the set, removes the .good and .bad substrings, and creates a new set. It’s clean, fast, and fits nicely into Pythonic conventions.

Great suggestion above! If you prefer something more step-by-step, especially for situations where set comprehension might seem tricky, you can use a for-loop to update the set directly. Here’s how:

set1 = {'Apple.good', 'Orange.good', 'Pear.bad', 'Pear.good', 'Banana.bad', 'Potato.bad'}

# Iterate and update the set
for x in set1.copy():  # Use a copy to avoid modifying the set while iterating
    set1.remove(x)
    set1.add(x.replace('.good', '').replace('.bad', ''))

print(set1)

The trick here is iterating over a copy of the set, ensuring we don’t run into modification errors. This approach is a bit more verbose but provides flexibility if you need extra operations later.

Both the solutions above work great! But if you want your code to be reusable and a little more structured, consider defining a function to handle the substring removal. Here’s how:

set1 = {'Apple.good', 'Orange.good', 'Pear.bad', 'Pear.good', 'Banana.bad', 'Potato.bad'}

# Define a function to remove substrings
def remove_substrings(s):
    return s.replace('.good', '').replace('.bad', '')

# Apply the function to each element in the set
set1 = {remove_substrings(x) for x in set1}

print(set1)

This way, your logic for removing substrings is neatly encapsulated in a function (remove_substrings()), making it easier to test or reuse elsewhere. Plus, the combination of a function and set comprehension strikes a great balance between readability and efficiency.