TransWikia.com

list assignment index out of range in a list array

Stack Overflow Asked by Marc833 on January 18, 2021

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!

3 Answers

Let's step through your code.

matrix = [list() for x in range(10)]

At this point, matrix is a list containing 10 empty lists. Now, let's consider the first iteration of your for loop.

for i in range (10): # i will be 0
    matrix[i] = [0 for x in range(3)] # matrix is now a list of
                                      # (one list of 3 zero's),
                                      # and 9 empty lists
    matrix[2][0] = 1 # remember that the rest of the elements in matrix
                     # are still empty lists. matrix[2] is an empty list,
                     # so you can't access matrix[2][0], as that would be
                     # getting the 0th index of an empty list.

Correct answer by ElectricShadow on January 18, 2021

That error occurred because you have no data in the list inside the list. That means there are no elements inside lists in your list. So you can't get one element from anything. The error is your first list comprehension. You didn't append values. I corrected it.

matrix = [[x] for x in range(10)]
for i in range(10):
    matrix[i] = [0 for x in range(3)]
    matrix[2][0] = 1
print(matrix)

I think you need to assign values in your empty lists. you can not do that using the equal sign. You need to insert elements using insert method instead of assign. like the following example

matrix = [list() for x in range(10)]
for i in range(10):
    matrix[i] = [0 for x in range(3)]
    matrix[2].insert(0, 1)
print(matrix)

This is how I understood your problem. If you can do more clarification, I can help more.

Answered by vsrukshan on January 18, 2021

Pay attention to indentation.

What you are doing is: you define a list of lists

matrix = [list() for x in range(10)]

For 10 times you are going to insert a list to the i-th element of the matrix and, in the same cicle, you are going to initialize an element to 1, but this element. But this element at the first run of the cicle has not been initialized, so when you do matrix[2][0] you get the index out of range execption

for i in range (10):
    matrix[i] = [0 for x in range(3)]
    matrix[2][0] = 1

try this instead:

for i in range (10):
    matrix[i] = [0 for x in range(3)]
matrix[2][0] = 1

Answered by tia.milani on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP