[Solved] Python: For loop not working properly


import re

rx = ['abc', 'de fg', 'hg i']

def string_m(a=rx):
    new = []
    for i in a:
        l = re.sub(" ","https://stackoverflow.com/",i)
        r = l.split("https://stackoverflow.com/")
        #r.reverse()
        rx = " ".join(r)
        new.append(rx)
    new.reverse()
    return new

a=string_m(rx)
print(a)

Your biggest problem is that you return the result of the first iteration of your for loop instead of after the for loop has finished.

Another issue is that strings cannot be reversed with the reverse() method. If you want to reverse the output, Try the above.

1

solved Python: For loop not working properly