Global list is appending nothing
<pre><code>answers = []

def search(visit_order, nodes_to_visit, distance):

    if len(nodes_to_visit) == 0:
        print visit_order
        answers.append(visit_order)
        return
    else:
        for node in nodes_to_visit:
            nodes_to_visit.remove(node)
            visit_order.append(node)
            search(visit_order, nodes_to_visit, 0)
            visit_order.remove(node)
            nodes_to_visit.append(node)

search([],nodes, 0)
print answers
</code></pre>

<p>I have a global list <code>answers</code> and a recursive function that goes through given <code>nodes_to_visit</code> list which will add <code>visit_order</code> to the <code>answers</code> list when there are no more <code>nodes_to_visit</code>. </p>

<p>When I print <code>Visit_order</code> right before appending, I get a correct value. However, when I print <code>answers</code>, I only get list of lists such as <code>[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]</code>. What is the problem?</p>

<p>For example, if I give search([],[1,2,3,4],0) as the input it is supposed to give me something like 
[[3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4], [3, 1, 2, 4]] 
but it gives me [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []] instead.</p>

<p>So the problem is you are appending <em>the same object</em> to <code>answers</code> which you then empty. Check the output of <code>[id(e) for e in answers]</code> and you should see the same object ids. A quick fix is to append <em>a copy</em> by using <code>answers.append(list(visit_order))</code> or <code>answers.append(visit_order[:])</code></p>

<pre><code>In [4]: search([],[1,2,3,4],0)

In [5]: answers
Out[5]: 
[[],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 []]

In [6]: [id(e) for e in answers]
Out[6]: 
[140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400]

In [7]:
</code></pre>

<p>However, if I change the function to:</p>

<pre><code>def search(visit_order, nodes_to_visit, distance):

    if len(nodes_to_visit) == 0:
        answers.append(visit_order[:])
        return
    else:
        for node in nodes_to_visit:
            nodes_to_visit.remove(node)
            visit_order.append(node)
            search(visit_order, nodes_to_visit, 0)
            visit_order.remove(node)
            nodes_to_visit.append(node)
</code></pre>

<p>Now...</p>

<pre><code>In [8]: answers = []

In [9]: search([],[1,2,3,4],0)

In [10]: answers
Out[10]: 
[[1, 2, 3, 4],
 [1, 2, 3, 4],
 [1, 3, 4, 2],
 [1, 3, 4, 2],
 [1, 3, 2, 4],
 [1, 3, 2, 4],
 [2, 4, 3, 1],
 [2, 4, 3, 1],
 [2, 3, 1, 4],
 [2, 3, 1, 4],
 [2, 3, 4, 1],
 [2, 3, 4, 1],
 [3, 1, 4, 2],
 [3, 1, 4, 2],
 [3, 4, 2, 1],
 [3, 4, 2, 1],
 [3, 4, 1, 2],
 [3, 4, 1, 2],
 [3, 2, 1, 4],
 [3, 2, 1, 4],
 [3, 1, 4, 2],
 [3, 1, 4, 2],
 [3, 1, 2, 4],
 [3, 1, 2, 4]]

In [11]: 
</code></pre>

python list global