-3

When I run my code, I receive this IndexError:

constraints[fr][to] = self.extra_constr[2]
IndexError: list index out of range

Here a sample of my code:

def determine_constr(self, parent_constr):
    constraints = copy.deepcopy(parent_constr)
    if self.extra_constr == None:
        return constraints

    fr = self.extra_constr[0]
    to = self.extra_constr[1]
    constraints[fr][to] = self.extra_constr[2]
    constraints[to][fr] = self.extra_constr[2]

    for i in range(2):
        constraints = self.removeEdges(constraints)
        constraints = self.addEdges(constraints)

    return constraints
Charles
  • 3,116
  • 2
  • 11
  • 20
  • This just means that your index is referencing an element beyond the end of the list. So, check your lists to see which one it is. For example, `self.extra_constr` must have length at least 3 to use index 2. Does it? Check it. – Tom Karzes Feb 09 '19 at 19:46

1 Answers1

3

You get this error because either

  • fr is out of range for constraints
  • to is out of range for constraints[fr]
  • or 2 is out of range for self.extra_constr

Without more details as to what these variables contain, it's hard to answer your question any more precisely.

You can double check which one of them is causing the error by using assertions in your code. For instance:

assert fr in range(len(constraints))
assert to in range(len(constraints[fr]))
assert 2 in range(len(self.extra_constr))

These will throw an AssertionError if the indexes are out of range, and help you debug your code!

Charles
  • 3,116
  • 2
  • 11
  • 20