Confused as to where to use return statement in Python
<p>Sometimes I get confused as to <em>where</em> to use the return statement. I've done the Python track on codecademy and I'm halfway through an Intro to CS class online and <em>still</em> I get it wrong sometimes. I get what it does, it's just that I don't 'get' its placement properly :/</p>

<p>Here's a short example of the same code. </p>

<p>Correct way:</p>

<pre><code>def product_list(list_of_numbers):
    c = 1
    for e in list_of_numbers:
        c = c * e
    return c
</code></pre>

<p>Wrong way (which I did initially):</p>

<pre><code>def product_list(list_of_numbers):
    c = 1
    for e in list_of_numbers:
        c = c * e
        return c
</code></pre>

<p>This probably sounds silly to most of you, but in bigger and more complicated situations I get it properly confused. If someone can clarify it to me once and for all I'd appreciate that a lot.</p>

<p>You're putting too much emphasis on the impact of <code>return</code> on controlling the behaviour of the <code>for</code> loop. Instead, <code>return</code> applies to the function and happens to terminate the <code>for</code> loop prematurely by primarily bringing an end to the function. </p>

<p>Instead, you can control the behaviour of the <code>for</code> loop independently from the function itself using <code>break</code>. In addition, you can have multiple <code>return</code> statements in a function depending on what action should be taken in response to particular criteria (as in <code>my_func1</code>). Consider the following:</p>

<pre><code>import random

def my_func1(my_list, entry):
    ''' 
    Search a list for a specific entry. When found, terminate search 
    and return the list index immediately

    Return False if not found
    '''

    print "\n Starting func1"
    index = 0

    for item in my_list:
        if item != entry:
            print "Not found yet at index: {}".format(index)
            index += 1
        else:
            print "found item, at index {}".format(index)
            print "Terminating function AND loop at same time"
            return index

    print "########### ENTRY NOT IN LIST. RETURN FAlSE #############"
    return False


a = my_func1(['my', 'name', 'is', 'john'], 'is')
b = my_func1(['my', 'name', 'is', 'john'], 'harry')


def my_func2(my_list):
    ''' Iterate through a list
    For first 4 items in list, double them and save result to a list that will
    be returned, otherwise terminate the loop
    Also, return another list of random numbers
    '''
    print '\n starting func2'

    return_list = []
    for i in range(len(my_list)):
        if i &lt; 4:
            print 'Value of i is {}'.format(i)
            return_list.append(my_list[i] * 2)
        else:
            print 'terminating for loop, but ** keep the function going **'
            break

    other_list = [random.randint(1, 10) for x in range(10)]

    print 'Returning both lists'
    return return_list, other_list

c = my_func2([x for x in range(10)])
</code></pre>

python