title: Changing variables inside a list in python (backtracking)
body: <p>I'm trying to solve a sudoku puzzle using python for that I'm using a 3D list, 2 dimensions for the sudoku board and a 3rd dimension to store the changes on the board and to be able to backtrack whenever a given cell cant be filled. I've written some code but I've reached a problem that I cant figure out, whenever the program assigns a number to a cell in the 3D list, it also changes the number in other cells. My code is as follows:</p>
  <pre><code>import math
  
  
  def square_id(grid, ni, nj):
      if ni &gt;= 9:
          return (&quot;the matrix doesn't have the right size or component doesn't exist&quot;)
      elif nj &gt;= 9:
          return (&quot;the matrix doesn't have the right size or component doesn't exist&quot;)
      elif ni &lt; 3:
          if nj &lt; 3:
              return 1
          elif nj &lt; 6:
              return 2
          elif nj &lt; 9:
              return 3
      elif ni &lt; 6:
          if nj &lt; 3:
              return 4
          elif nj &lt; 6:
              return 5
          elif nj &lt; 9:
              return 6
      elif ni &lt; 9:
          if nj &lt; 3:
              return 7
          elif nj &lt; 6:
              return 8
          elif nj &lt; 9:
              return 9
  
  
  def sudo_rules(grid, ni, nj):
      if grid[ni][nj] == 0:
          return False
      for i in range(0, 9):
          if grid[i][nj] == grid[ni][nj] and i != ni:
              return False
      for j in range(0, 9):
          if grid[ni][j] == grid[ni][nj] and j != nj:
              return False
      for i in range(0, 3):
          for j in range(0, 3):
              k = square_id(grid, ni, nj)
              l = int(math.ceil(k / 3)) - 1
              c = k % 3 - 1
              if c == -1:
                  c = 2
              if grid[3 * l + i][3 * c + j] == grid[ni][nj] and 3 * c + j != nj and 3 * l + i != ni:
                  return False
  
  
  grid0 = [[0, 0, 0, 6, 0, 0, 0, 8, 0],
          [0, 4, 0, 0, 0, 0, 0, 2, 0],
          [5, 0, 0, 0, 9, 0, 4, 0, 6],
          [0, 5, 0, 2, 0, 0, 7, 0, 1],
          [9, 0, 0, 0, 0, 3, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 4, 0],
          [0, 0, 1, 0, 2, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 8],
          [0, 7, 0, 1, 0, 0, 6, 0, 5]]
  
  grid= []
  pos = []
  for i in range(0, 30):
      grid.append([[0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0]])
      pos.append([1, 0, 0])
  
  grid[0]=grid0
  
  k = 0
  i = 0
  j = 0
  n = 1
  
  while i &lt; 9:
      if j == 9:
          j = 0
      while j &lt; 9:
          if sudo_rules(grid[k], i, j) == False:
              while n &lt; 11:
                  if n == 10:
                      break
                  print(k, i, j)
                  print(&quot;grid[0]:&quot;, grid[0])
                  grid[k][i][j] = n
                  print(&quot;grid[0]:&quot;, grid[0])
                  print(&quot;grid[1]:&quot;, grid[1])
                  if sudo_rules(grid[k], i, j) != False:
                      pos[k] = [n, i, j]
                      n = 1
                      grid[k + 1] = grid[k]
                      k = k + 1
                      break
                  n = n + 1
                  if k==1:
                      exit()
          if n == 10:
              break
          j = j + 1
      i = i + 1
      if n == 10:
          print(grid[0])
          k = k - 1
          n = pos[k][0] + 1
          i = pos[k][1]
          j = pos[k][2]
  
  for i in range(0, 9):
      for j in range(0, 9):
          if sudo_rules(grid[k], i, j) == False:
              print(&quot;Wrong&quot;)
              break
  </code></pre>
  <p>My problem is in this line:</p>
  <pre><code>                print(k, i, j)
                  print(&quot;grid[0]:&quot;, grid[0])
                  grid[k][i][j] = n
                  print(&quot;grid[0]:&quot;, grid[0])
                  print(&quot;grid[1]:&quot;, grid[1])
  </code></pre>
  <p>I dont understand why changing grid[1][0][1] also changes grid[0][0][1]. Any help would be appreciated, thank you</p>