Hick
Hick

Reputation: 36394

Python error: IndexError: list assignment index out of range

a=[]
a.append(3)
a.append(7)

for j in range(2,23480):
    a[j]=a[j-2]+(j+2)*(j+3)/2

When I write this code, it gives an error like this:

Traceback (most recent call last):
  File "C:/Python26/tcount2.py", line 6, in <module>
    a[j]=a[j-2]+(j+2)*(j+3)/2
IndexError: list assignment index out of range

May I know why and how to debug it?

Upvotes: 2

Views: 8944

Answers (7)

hao
hao

Reputation: 10228

Change this line of code:

a[j]=a[j-2]+(j+2)*(j+3)/2

to this:

a.append(a[j-2] + (j+2)*(j+3)/2)

Upvotes: 7

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391326

The reason for the error is that you're trying, as the error message says, to access a portion of the list that is currently out of range.

For instance, assume you're creating a list of 10 people, and you try to specify who the 11th person on that list is going to be. On your paper-pad, it might be easy to just make room for another person, but runtime objects, like the list in python, isn't that forgiving.

Your list starts out empty because of this:

a = []

then you add 2 elements to it, with this code:

a.append(3)
a.append(7)

this makes the size of the list just big enough to hold 2 elements, the two you added, which has an index of 0 and 1 (python lists are 0-based).

In your code, further down, you then specify the contents of element j which starts at 2, and your code blows up immediately because you're trying to say "for a list of 2 elements, please store the following value as the 3rd element".

Again, lists like the one in Python usually aren't that forgiving.

Instead, you're going to have to do one of two things:

  1. In some cases, you want to store into an existing element, or add a new element, depending on whether the index you specify is available or not
  2. In other cases, you always want to add a new element

In your case, you want to do nbr. 2, which means you want to rewrite this line of code:

a[j]=a[j-2]+(j+2)*(j+3)/2

to this:

a.append(a[j-2]+(j+2)*(j+3)/2)

This will append a new element to the end of the list, which is OK, instead of trying to assign a new value to element N+1, where N is the current length of the list, which isn't OK.

Upvotes: 3

ojrac
ojrac

Reputation: 13421

If you want to debug it, just change your code to print out the current index as you go:

 a=[]
 a.append(3)
 a.append(7)

 for j in range(2,23480):
      print j # <-- this line
      a[j]=a[j-2]+(j+2)*(j+3)/2

But you'll probably find that it errors out the second you access a[2] or higher; you've only added two values, but you're trying to access the 3rd and onward.

Try replacing your list ([]) with a dictionary ({}); that way, you can assign to whatever numbers you like -- or, if you really want a list, initialize it with 23479 items ([0] * 23479).

Upvotes: 1

Aaron
Aaron

Reputation:

Python does not dynamically increase the size of an array when you assign to an element. You have to use a.append(element) to add an element onto the end, or a.insert(i, element) to insert the element at the position before i.

Upvotes: 0

Unknown
Unknown

Reputation: 46773

Python lists must be pre-initialzed. You need to do a = [0]*23480

Or you can append if you are adding one at a time. I think it would probably be faster to preallocate the array.

Upvotes: 0

tom10
tom10

Reputation: 69182

At j=2 you're trying to assign to a[2], which doesn't exist yet. You probably want to use append instead.

Upvotes: 1

Stephan202
Stephan202

Reputation: 61489

You're adding new elements, elements that do not exist yet. Hence you need to use append: since the items do not exist yet, you cannot reference them by index. Overview of operations on mutable sequence types.

for j in range(2, 23480):
    a.append(a[j - 2] + (j + 2) * (j + 3) / 2)

Upvotes: 7

Related Questions