Skip to content Skip to sidebar Skip to footer

IndexError: List Assignment Index Out Of Range - Python With An Array

I recently started to use python and i am still newbie with many things of the language. This piece of code should print a serie rows (Ex.:[47.815, 47.54, 48.065, 57.45]) that i ta

Solution 1:

It seems like your problem is pretty straightforward. You probably have a list named row and you're trying to assign values to non-existent indices

>>> row = []
>>> row[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

If however you appended a new list into the row list like I think you want to do

>>> row = []
>>> row.append([1, 2, 3, 4])
>>> row
[[1, 2, 3, 4]]
>>> row[0][0]
1

This would work fine. If you're trying to triple nest things

>>> row = []
>>> row.append([[1,2,3,4]])
>>> row
[[[1, 2, 3, 4]]]
>>> row[0].append([-1, -2, -3, -4])
>>> row
[[[1, 2, 3, 4], [-1, -2, -3, -4]]]

You can use the same syntax


Post a Comment for "IndexError: List Assignment Index Out Of Range - Python With An Array"