2 Replies - 1544 Views - Last Post: 14 December 2011 - 10:18 PM Rate Topic: -----

#1 MangoTux  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 06-April 11

List index out of bounds, but I initialized?

Posted 14 December 2011 - 09:01 PM

array1 = [[2],[10]]
array2 = [10]
array3 = [10]

textIn = f.read() #f is a file with 20 values
textSplit = textIn.split()
for x in range(20):
    textSplit[x] = textSplit[x].strip(',') #Removes , from values

for y in range(10):
    for x in range(2):
        array[x][y] = textSplit[(2*y)*(x+1)+x]



For some reason, I keep getting a list index out of bounds on the line with the
array[x][y] = textSplit[(2*y)*(x+1)+x]

line in the code. I'm sure that I've initialized it, but I'm just confused now. This is my 2nd day with Python.

Is This A Good Question/Topic? 0
  • +

Replies To: List index out of bounds, but I initialized?

#2 MangoTux  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 06-April 11

Re: List index out of bounds, but I initialized?

Posted 14 December 2011 - 09:07 PM

I tried initializing the list with 0's, but it still got the same error.
array = [[3],[10]]
for x in range(3):
    for y in range(10):
        array[x][y] = 0

Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: List index out of bounds, but I initialized?

Posted 14 December 2011 - 10:18 PM

If textSplit has 20 elements, they will be numbered 0-19. At their max, y will be 9, and x will be 1, leaving (2*y)*(x+1)+x = 37, well outside the range of available indexes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1