Error - list index out of range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Einat
    New Member
    • Aug 2010
    • 2

    Error - list index out of range

    Hi all,

    I'm just starting my way into Python and I'm trying to use a while loop with lists but keep getting the list index out of range error.
    (Please ignore any other foolish mistakes I might find later on J):

    Code:
      DBReportsArray = [6000,5000,4000,3000,2000,1000,100]         
      curNum = 0 
      RepInDB = tssh.command ("ls -l /something | wc -l")[0] ##something to get a result by ssh
      RepInDB = [y.replace("\n","") for y in RepInDB] ##replaces a non-good format
      RepInDB = RepInDB[0]
      print RepInDB ##I got 351
      print self.DBReportsArray[curNum] 
      while self.DBReportsArray[curNum] <= RepInDB: 
         curNum+=1
      RepToChange = RepInDB-self.DBReportsArray[self.curNum]
      print RepToChange
    Thanks in advance,
    Einat
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Einat,

    Either RepInDb is an empty list, or the value of curNum is equal to or exceeds the length of the list self.DBReportsA rray as in:
    Code:
    [I]>>> [][0]
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    IndexError: list index out of range
    >>> alist = [0,1,2,3,4,5]
    >>> alist[0]
    [I]0[/I]
    >>> alist[5]
    5
    >>> alist[6]
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
    IndexError: list index out of range
    >>> [/I]

    Comment

    • Einat
      New Member
      • Aug 2010
      • 2

      #3
      workaround

      Appreciate your reply.

      I tried verifying the content of RepInDb and curNum and both were in order.
      Anyway I converted the "while" loop to a "for" loop and now it works:
      Code:
      DBReportsArray = [6000,5000,4000,3000,2000,1000,100]         
      curNum = 0 
      RepInDB = tssh.command ("ls -l /something | wc -l")[0] ##something to get a result by ssh
      RepInDB = [y.replace("\n","") for y in RepInDB] ##replaces a non-good format
      RepInDB = RepInDB[0]
      print RepInDB
      for num in range(len(self.DBReportsArray)):
          if self.DBReportsArray[num] <= RepInDB:
             break
          return num,RepInDB
      Thanks again,
      E
      Last edited by Einat; Aug 29 '10, 05:50 AM. Reason: forgot to mention something

      Comment

      Working...