["python"]
How to remove specific substrings from a set of strings in Python?

I have a set of strings and all the strings have one of two specific substrings which I want to remove: set1 = {'Apple.good', 'Orange.good', 'Pear.bad', 'Pear.good', 'Banana.bad', 'Potato.bad'} I want the ".good" and ".bad" substrings removed from all the strings. I tried this: for x in set1: x.replace('.good', '') x.replace('.bad', '') but it doesn't seem to work, set1 stays exactly the same. I tried using for x in list(set1) instead but that doesn't change anything.
Strings are immutable. str.replace creates a new string. This is stated in the documentation: str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new. [...] This means you have to re-allocate the set or re-populate it (re-allocating is easier with a set comprehension): new_set = {x.replace('.good', '').replace('.bad', '') for x in set1} P.S. if you're using Python 3.9 or newer, use str.removeprefix() or str.removesuffix() instead: new_set = {x.removesuffix('.good').removesuffix('.bad') for x in set1}