title: Redundant Elements Appear in Result when Executing Nested For-loop in Python
body: <p>I'm writing a program in Python that fetches all possible first moves for X and O. In this program, X goes first and O goes second. Once X and O finish their moves, I want to print all possible states that the game board can be in. </p>
  
  <p>Initially, X can make nine moves since there are nine empty positions on the board; then, O can make eight moves since there are eight empty positions left.
  So, when I print the results, I should get 72 results. Instead, I am getting 108 results.</p>
  
  <p>This is because after getting the first move for X, for some reason that I can't figure out, the previous moves for X gets added to the nextStates.nextStates.
  Thus, instead of getting 9*8 results, I get (0 + 8) + (1 + 8) + ... + (8 + 8) = 108 results.</p>
  
  <p>I don't know why this is the case since as far as I'm concerned, I'm just using an equivalent of a double for-loop.</p>
  
  <pre><code>import copy
  
  class GameBoard:
  
    def __init__ (self):
      self.SIZE = 3
      self.currentState = [
          [' ', ' ', ' '],
          [' ', ' ', ' '],
          [' ', ' ', ' ']]
      self.nextStates = []
  
  EMPTY = ' '
  X = 'X'
  O = 'O'
  
  def printBoard(gameBoard):
    for row in range(0, gameBoard.SIZE):
      for col in range(0, gameBoard.SIZE):
        print('[' + gameBoard.currentState[row][col] + ']', end = '')
      print()
    print()
  
  def getNextStatesO(gameBoard):
    for row in range(0, gameBoard.SIZE):
      for col in range(0, gameBoard.SIZE):
        if(gameBoard.currentState[row][col] == EMPTY):
          tempBoard = copy.deepcopy(gameBoard)
          tempBoard.currentState[row][col] = O
          gameBoard.nextStates.append(tempBoard)
    return gameBoard
  
  def getNextStatesX(gameBoard):
    for row in range(0, gameBoard.SIZE):
      for col in range(0, gameBoard.SIZE):
        if(gameBoard.currentState[row][col] == EMPTY):
          tempBoard = copy.deepcopy(gameBoard)
          tempBoard.currentState[row][col] = X
          gameBoard.nextStates.append(tempBoard)
    return gameBoard
  
  def getNextStates(gameBoard):
    gameBoard = getNextStatesX(gameBoard)
    for i in range(0, len(gameBoard.nextStates)):
      gameBoard.nextStates[i] = getNextStatesO(gameBoard.nextStates[i])
    return gameBoard
  
  def main():
    gameBoard = GameBoard()
    gameBoard = getNextStates(gameBoard)
    counter = 1
    for x in range(0, len(gameBoard.nextStates)):
      for o in range(0, len(gameBoard.nextStates[x].nextStates)):
        print(str(counter) + ' X: ' + str(x) + ' O: ' + str(o))
        printBoard(gameBoard.nextStates[x].nextStates[o])
        counter += 1
  
  main()
  </code></pre>