2

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!

4
  • 3
    On the first iteration of your loop, matrix[2] is still an empty list (from the original initialization of matrix). There is no index that you can use with an empty list. I have no idea what matrix[2][0] = 1 is supposed to accomplish, anyway, since you will be entirely replacing matrix[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? Commented Nov 23, 2020 at 21:21
  • @mkrieger1 they did Commented Nov 23, 2020 at 21:22
  • What is the desired output from your code? It doesn't look like you are able to understand lists. Your first line will result in [[], [], [], [], [], [], [], [], [], []]. 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. Commented Nov 23, 2020 at 21:28
  • This is because you access matrix[2][0] before you initialize matrix[2] with the list of 3 elements. If you do that outside the loop verything will be fine. Commented Nov 23, 2020 at 22:15

3 Answers 3

3

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.
Sign up to request clarification or add additional context in comments.

3 Comments

updated from 8 empty lists to 9 empty lists as the total elements is 10.
@JoeFerndz Thanks for catching that!
Thank you, so much for your answer. I understand that I have made a very stupid mistake; I just shouldn't insert the "matrix[2][0] = 1" into the for-loop. Then it works, as I wanted it. Thank you agains! :-)
3

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

2 Comments

Thank you so, much. You got exactly the point. I have made such a stupid mistake.
If I answered your problem can you make my solution is the answer? Only if my solution is the answer. Happy to see that you solved the problem
2

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.

3 Comments

OP didn't want a 10 x 10 matrix with increasing rows, but a 10 x 3 containing all 0's.
I edited it for eliminating the error. I didn't understand the requirement. Thank you for the clarification.
Thank you very much! My problem is already solved - please see above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.