title: List index out of range in 2D list
body: <p>I understand that there are similar questions to this <a href="https://stackoverflow.com/questions/1798796/python-list-index-out-of-range-error">here</a>, <a href="https://stackoverflow.com/questions/6667201/how-to-define-two-dimensional-array-in-python">here</a>, and <a href="https://stackoverflow.com/questions/13157961/2d-array-of-zeros">here</a>. The first one addresses 1D lists, the second is great except it doesn't seem to work, and the third is close, but I still don't quite understand my issue.</p>
  
  <p>Here is what I am trying to do. I need to create an 2D list (a 2D array in java and C++, which I am much more familiar with) filled with 0's. It needs to be size 20 across and 15 down.</p>
  
  <p>Here is what I have tried:</p>
  
  <pre><code>self.grid = [[0 for x in range(GRID_COLUMN_SIZE)] for y in range(GRID_ROW_SIZE)] # where GRID_ROW_SIZE = 15, GRID_COLUMN_SIZE = 20
  </code></pre>
  
  <p><em>Note</em>, I tried with the two constants switched (<code>COLUMN</code> first, then <code>ROW</code>), and it broke slightly later. In addition, I print the 2D list out and it's the wrong dimensions (15 across and 20 down).</p>
  
  <p>Here is my later use of <code>self.grid</code>. Without getting too deep, I am iterating through all the values of the list (<code>grid</code>) and getting the surrounding points.</p>
  
  <pre><code>def populatePaths(self):
      for row in range(len(self.grid)):
          for column in range(len(self.grid[row])):
              if self.isPointAccessible(column, row):
                  self.addPaths(column, row)
  
  def addPaths(self, x, y):
      key = Point(x, y)
      print "Each: %s" % (key.toString())
      points = key.getSurroundingPoints()
      self.removeBarriersFromPath(points)
      self.paths[key] = points # a map from Points to lists of surrounding Points
  </code></pre>
  
  <p>Basically, I remove points along the path that can't be reached:</p>
  
  <pre><code>def removeBarriersFromPath(self, path):
      for point in list(path):
          print "Surrounding %s" % (point.toString())
          if not self.isPointAccessible(point.x, point.y):
              path.remove(point)
      return path
  </code></pre>
  
  <p><code>self.isPointAccessible()</code> is trivial, but this is where it breaks. It checks to see if the value at the <code>(x,y)</code> location is 0: <code>return self.grid[x][y] == 0</code>  </p>
  
  <p>I added these print statements (<code>point.toString()</code> returns <code>(x,y)</code>) to show me the points as they happen, and I am able to iterate until <code>x==14</code>, but it breaks at <code>x==15</code>.</p>
  
  <p>I suspect that I am getting the column/row order in the looping incorrect, but I'm not sure when/how.</p>
  
  <p>Let me know if I didn't explain something clearly enough.</p>
  
  <p><strong>Edit</strong> Here is the traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 70, in start
      self.populatePaths()
    File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 156, in populatePaths
      self.addPaths(column, row)
    File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 162, in addPaths
      self.removeBarriersFromPath(points)
    File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 168, in removeBarriersFromPath
      if not self.isPointAccessible(point.x, point.y):
    File "/home/nu/catkin_ws/src/apriltags_intrude_detector/scripts/sphero_intrude_gui.py", line 173, in isPointAccessible
      return self.grid[x][y] == 0
  IndexError: list index out of range
  </code></pre>