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

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página