I try to make my first list-array.
matrix = [list() for x in range(10)]
for i in range (10):
matrix[i] = [0 for x in range(3)]
matrix[2][0] = 1
print (matrix)
If I want to change the value in a cell higher than zero in the first index bracket like [2][0] then I receive an "list assignment index out of range" error. I can change values in the cells [0][0], [0][1] and [0][2] but not in cells [1][x] or with a higher value in the first bracket. I really don't understand my mistake, due to all the cells up to [9][2] seems to defined if i print them, as far as I know.
Thank you very much for your help!
matrix[2]is still an empty list (from the original initialization ofmatrix). There is no index that you can use with an empty list. I have no idea whatmatrix[2][0] = 1is supposed to accomplish, anyway, since you will be entirely replacingmatrix[2]on the third iteration of the loop. Maybe that line is not supposed to be indented, so that it runs after the loop finishes?[[], [], [], [], [], [], [], [], [], []]. Did you try to do a print statement before the for loop to see what gets produced? I recommend using print statements to debug your code. That will help you understand the values before assignments are done.