title: Why global list is automatically adds variables without explicit add statement?
body: <p>I defined the visited_before list as a global list, but the issue is that; after making debugging, new variables are added implicitly to this list which made the whole code useless. If there is a possible tricky solution or tip I would be thankful to you.</p>
  <p>Here is my code:</p>
  <pre><code>global visited_before
  
  
  
  def give_next(initial):
      for block in initial:
          '''
          If block is empty move to the next block
          '''
          if not block:
              continue # change the block
          if block:
              packet = block[0]
              block.remove(packet)
          '''
          After this step, a disk is selected to be moved
          '''
          for position in initial:
              '''
          There are three cases:
          1. position is empty                        So add at index 0
          2. position is full with elements &gt; packet  So add at index 0
          3. position is full with elements &lt; packet  So Change the position   
              '''
              if block == position:
                  continue # Don't return the disk to the first place !!
              elif not position:
                  position.insert(0,packet)
              elif position[-1] &gt; packet:
                  position.insert(0,packet)
              elif position[-1] &lt; packet:
                  continue # Not allowed movment, SO change the block
  
              '''
              Check if the predicted step is in the visited_before list:
              if YES: change the block, in a try to predict a new step
              if  NO: add the step to the visited_before list and finish 
              '''
              if initial in visited_before:
                  continue
              elif initial not in visited_before:
                  visited_before.insert(-1, initial)
                  print(initial)
                  return initial
  
  
  start_state = [[1,2,3],[],[]]
  visited_before = [start_state]
  give_next(start_state)
  </code></pre>