Python: For loop not working properly
<p>Why is the for loop not working properly? It's just returned only one element.</p>

<p>1.</p>

<pre><code>rx = ['abc', 'de fg', 'hg i']
def string_m(a=rx):
     for i in a:
         l = re.sub(" ","/",i)
         r = l.split("/")
         r.reverse()
         rx = " ".join(r)
         return rx
a=string_m(rx)
print(a)
</code></pre>

<p>Outputs:</p>

<pre><code>abc
</code></pre>

<p>2.</p>

<pre><code>rx = ['abc', 'de fg', 'hg i']
def string_m(a=rx):
     for i in a:
         l = re.sub(" ","/",i)
         r = l.split("/")
         r.reverse()
         rx = " ".join(r)
     return rx
a=string_m(rx)
print(a)
</code></pre>

<p>Outputs:</p>

<pre><code>i hg
</code></pre>

<p>-- Can someone help me to see what I am doing wrong?</p>

<pre><code>import re

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

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

a=string_m(rx)
print(a)
</code></pre>

<p>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.</p>

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

python django