title: Global list mutation inside function in Python
body: <p>I am working on a backtracking solution for this problem - "Given a string s, partition s such that every substring of the partition is a palindrome."</p>
  
  <p>I have written this code where I am not able to get <strong>how is the global 2D list strings is getting updated?</strong> What exactly is happening here? I tried using global keyword too with it inside palinBreak function, but it doesn't help! <strong>When should global keyword be used?</strong></p>
  
  <p><strong>Observation:</strong> Every element of global list strings changes to local list variable arr. For instance, strings = [x, y] and arr = [z], then strings becomes [z, z, z]; whereas I want it to be [x, y, z]. Why does this happen?</p>
  
  <p><strong>EDIT: Adding expected output</strong> vs. output I am getting (take notice from line 3 onwards).</p>
  
  <p>Expected output is:</p>
  
  <pre><code>ans is ['a', 'b', 'a', 'a', 'b'] []
  strings is [['a', 'b', 'a', 'a', 'b']]
  ans is ['a', 'b', 'aa', 'b'] [['a', 'b', 'a', 'a', 'b']]
  strings is [['a', 'b', 'a', 'a', 'b'], ['a', 'b', 'aa', 'b']]
  ans is ['a', 'baab'] [['a', 'b', 'a', 'a', 'b'], ['a', 'b', 'aa', 'b']]
  strings is [['a', 'b', 'a', 'a', 'b'], ['a', 'b', 'aa', 'b'], ['a', 'baab']]
  ans is ['aba', 'a', 'b'] [['a', 'b', 'a', 'a', 'b'], ['a', 'b', 'aa', 'b'], ['a', 'baab']]
  strings is [['a', 'b', 'a', 'a', 'b'], ['a', 'b', 'aa', 'b'], ['a', 'baab'], ['aba', 'a', 'b']]
  [['a', 'b', 'a', 'a', 'b'], ['a', 'b', 'aa', 'b'], ['a', 'baab'], ['aba', 'a', 'b']]
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>ans is ['a', 'b', 'a', 'a', 'b'] []
  strings is [['a', 'b', 'a', 'a', 'b']]
  ans is ['a', 'b', 'aa', 'b'] [['a', 'b', 'aa', 'b']]
  strings is [['a', 'b', 'aa', 'b'], ['a', 'b', 'aa', 'b']]
  ans is ['a', 'baab'] [['a', 'baab'], ['a', 'baab']]
  strings is [['a', 'baab'], ['a', 'baab'], ['a', 'baab']]
  ans is ['aba', 'a', 'b'] [['aba', 'a', 'b'], ['aba', 'a', 'b'], ['aba', 'a', 'b']]
  strings is [['aba', 'a', 'b'], ['aba', 'a', 'b'], ['aba', 'a', 'b'], ['aba', 'a', 'b']]
  [[], [], [], []]
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>def isPalin(s):
      i = 0
      j = len(s)-1
      while(i&lt;j):
          if(s[i]!=s[j]):
              return False
          i+=1
          j-=1
      return True
  
  def palinBreak(s, start, arr):
      #print "Called", start, arr
      #global strings
      if(start==len(s)):
          print "ans is", arr, strings
          strings.append(arr)
          print "strings is", strings
          return 0
  
      flag = -1
      for i in range(1, len(s)-start+1):
          curr = s[start : start+i]
          #print "Testing curr and start and i", curr, start, i
          if(isPalin(curr)):
              arr.append(curr)
              #print arr, start, i
              #print "Next call from", start+i
              pb = palinBreak(s, start+i, arr)
              if(pb != -1):
                  flag = 1
              arr.pop()
              #print "popped l", arr
      return flag
  
  strings = []
  palinBreak("abaab", 0, [])
  print strings
  </code></pre>