[Python] In Python, how do I iterate operation over several lists and return an updated list...

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to iterate the same series of string-cleaning operations over several long lists. The functions in the loop do what I want and print the result I expect. But then when I access the lists outside of the loop, they're still the unmodified original. How can I ensure that the loop returns the updated list?

    I need to maintain them as separate lists for future operations. But I don't necessarily need to maintain the original list names (e.g., l1_clean would be fine).

    import re

    # Example lists
    l1 = ['abc', 'def', 'ghix', 'ghi']
    l2 = ['defx', 'def', 'ghi', 'jkl']

    # Example cleaning operation loop
    for listx in [l1, l2]:
    listx = [re.sub('x', '', i) for i in listx]
    listx = sorted(listx)
    listx = [set(listx)]
    print(listx)

    listx = listx # I know this isn't right, but I'm not sure what it should be instead


    This prints the new lists that I expect:

    >[{'abc', 'def', 'ghi'}]
    >[{'def', 'jkl', 'ghi'}]


    But then when I attempt to print the cleaned lists, I'm still getting the original:

    print(l1, l2)
    >['abc', 'def', 'ghix', 'ghi'] ['defx', 'def', 'ghi', 'jkl']

    Continue reading...

Compartilhe esta Página