title: How does putting the variable for empty list alter this python code?
body: <p>I am trying to write a code to flatten any nested list using recursion. This was the first code I wrote:</p>
  
  <pre><code>def flatten(NL):
      a = []
      for i in NL:
          if type(i) == int:
              a.append(i)
          elif type(i) == list:
              flatten(i)
      return a
  </code></pre>
  
  <p>However, it kept returning an empty list until I changed the code and put the variable "a" outside the function:</p>
  
  <pre><code>a = []
  def flatten(NL):
      for i in NL:
          if type(i) == int:
              a.append(i)
          elif type(i) == list:
              flatten(i)
      return a
  </code></pre>
  
  <p>I have tried to wrap my mind around it to no avail. How has putting the empty list outside the function affected it?</p>