Python list return 'None'
<p>Code:</p>

<pre><code>puzzle1= [
          [7,0,0,0,0,0,2,1,8],
          [0,4,8,6,2,9,0,0,0],
          [0,0,3,0,0,1,0,0,0],
          [0,0,7,0,0,8,0,3,2],
          [0,0,9,7,0,6,5,0,0],
          [6,8,0,1,0,0,7,0,0],
          [0,0,0,2,0,0,4,0,0],
          [0,0,0,4,1,5,8,7,0],
          [3,5,4,0,0,0,0,0,6]
          ]  
def eliminate_values(puzzle):
    redo = False
    for i in range(9):
        for j in range(9):
            if puzzle[i][j]==0 or isinstance(puzzle[i][j], list):
                puzzle[i][j] = []
                for num in range(1,10):
                   num_check = True;
                   for x in range(9):
                       if puzzle[i][x]==num:
                           num_check = False
                       if puzzle[x][j]==num:
                           num_check = False
                       if i&lt;3:
                           aa=0
                       elif i&lt;6 and i&gt;2:
                           aa=3
                       else:
                           aa=6
                       if j&lt;3:
                           bb=0
                       elif j&lt;6 and j&gt;2:
                           bb=3
                       else:
                           bb=6
                       for a in range(3):
                           for b in range(3):
                               if puzzle[a+aa][b+bb]==num:
                                   num_check = False
                   if num_check:
                       puzzle[i][j].append(num)
                if len(puzzle[i][j]) == 1:
                    puzzle[i][j] = puzzle[i][j][0]
                    redo = True;
    if redo:
        eliminate_values(puzzle)
    else:
        print(puzzle)
        return puzzle

puzzle=eliminate_values(puzzle1)
print(puzzle)
</code></pre>

<p>Console: </p>

<pre><code>[[7, 9, 6, 3, 5, 4, 2, 1, 8], [1, 4, 8, 6, 2, 9, 3, 5, 7], [5, 2, 3, 8, 7, 1, 9, 6, 4], [4, 1, 7, 5, 9, 8, 6, 3, 2], [2, 3, 9, 7, 4, 6, 5, 8, 1], [6, 8, 5, 1, 3, 2, 7, 4, 9], [8, 7, 1, 2, 6, 3, 4, 9, 5], [9, 6, 2, 4, 1, 5, 8, 7, 3], [3, 5, 4, 9, 8, 7, 1, 2, 6]]
None
</code></pre>

<p>Comments:</p>

<p>I am new to python but i dont understand why print IS working within the function and NOT after it is returned to the main program. (expecting it to print twice but only prints once then 'None')</p>

<p>If <code>redo</code> is <code>True</code>, you are recursively calling your function, and somewhere down the stack, once <code>redo</code> is <code>False</code>, you print and return the result. However, this result is not propagated up the call stack, thus the outermost function call will return nothing, i.e. <code>None</code>, which is then printed. For this, you also have to <code>return</code> the result of the recursive call:</p>

<pre><code>    if redo:
        return eliminate_values(puzzle)  # try again and return result
    else:
        return puzzle  # result found in this try, return it
</code></pre>

<p>Alternatively, instead of using recursion, you could wrap your function body in a <code>while</code> loop.</p>

python list return