title: Appending one list to another in recursion
body: <p>So the question was to find all paths from source to target (<a href="https://leetcode.com/problems/all-paths-from-source-to-target/" rel="nofollow noreferrer">https://leetcode.com/problems/all-paths-from-source-to-target/</a>).</p>
  <pre><code># code snippet 1
  
  class Solution:
      def allPathsSourceTarget(self, graph: List[List[int]]) -&gt; List[List[int]]:
          paths = []
          def dfsPath(node):
              if node == len(graph) - 1:
                  print(validPath) # line 6
                  paths.append(validPath) # line 7
                  return paths
              else:
                  for n in graph[node]:
                      validPath.append(n)
                      dfsPath(n)
                      validPath.pop()  
          validPath = [0]
          dfsPath(0)
          return paths        
  </code></pre>
  <p><a href="https://i.stack.imgur.com/BUkoY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BUkoY.png" alt="enter image description here" /></a></p>
  <pre><code># code snippet 2
  
  class Solution:
      def allPathsSourceTarget(self, graph: List[List[int]]) -&gt; List[List[int]]:
          paths = []
          def dfsPath(node):
              if node == len(graph) - 1:
                  print(validPath) # line 6
                  paths.append(validPath.copy()) # line 7
                  return paths
              else:
                  for n in graph[node]:
                      validPath.append(n)
                      dfsPath(n)
                      validPath.pop()  
          validPath = [0]
          dfsPath(0)
          return paths        
  </code></pre>
  <p><a href="https://i.stack.imgur.com/BieUP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BieUP.png" alt="enter image description here" /></a></p>
  <p>The only change in the above two code snippets is line 7. I've added the print statement in line 6 to show what <code>validPath</code> is holding. My question is why appending just <code>validPath</code>,  results in a different output than when I append <code>validPath.copy()</code> (in line 7).</p>