Matrix Problem Error in output

I wrote a code for Question 8(Matrix creation) of W3D2 coding assignment. Before reaching to the correct efficient code. I wrote some trial codes.
The Question is to create nested list: A = [[1,2,3],[4,5,6],[7,8,9]]

I wrote a preliminary code:
First Code:

outer_list=[[0,0,0],[0,0,0],[0,0,0]]

inner_list=[0,0,0]

for i in range(len(outer_list)):

    print(f"i= {i}")

    for j in range(len(inner_list)):

        print(f"j={j}")

        outer_list[i][j]=int(input())

print(outer_list)

This code gives me the correct result. It works

Second Trial:
I tried to use this code to define function that takes input from user for matrix dimension and matrix elements:

def initialise_matrix_2(inner,outer):

    outer_list=[]

    inner_list=[]

    for a in range(inner):

        inner_list.append(0)

    print(f"inner_list= {inner_list}")

    for b in range(outer):

        outer_list.append(inner_list)

    print(f"outer_list= {outer_list}")

    for i in range(len(outer_list)):

        #print(f"i={i}")

        for j in range(len(inner_list)):

            #print(f"j={j}")

            #print(len(outer_list))

            #print(outer_list[i][j])

            outer_list[i][j]=int(input())

    return outer_list

print(initialise_matrix_2(3,3))

I do not know why this code is not giving correct results, though I have tried more and written better code. But not sure why this code is not giving correct results.

Your first code is completely correct.

If you execute the second code and take inputs from 0 to 9. You will get the following matrix at the end

[[7, 8, 9], [7, 8, 9], [7, 8, 9]]

This is not the correct you would want the below output

[[1,2,3], [4,5,6], [7, 8, 9]]

Now the reason for this issue is related to the fact that when you are creating outer list in second code by appending the same inner list 3 time (let’s suppose length of outer list is 3), you are thinking that the inner list that you have appended 3 times is 3 different inner list. But these inner list is actually referencing to the same inner list variable. If you change one of them all of them will change.

To show this programmatically, let’s consider a small example

inner_list = [0,0,0]
outer_list = []
#let's suppose the length of outer list is 2
for i in range(2):
     outer_list.append(inner_list)
print(outer_list)

# now just try to change any element of the above outer list
outer_list[0][1] = 2

print(outer_list)

if you execute above code you would think the outer list will be

[[0,2,0],[0,0,0]]

but the result is

[[0, 2, 0], [0, 2, 0]]

this is because in the above code in this line

outer_list.append(inner_list)

when you are trying to append inner lists two times, it is actually creating a reference of the same inner list 2 times. It means that if you change one of them the other will also change.

To handle this situation properly you could change the above example code as below

outer_list = []
#let's suppose the length of outer list is 2 and length of inner list is 3
for i in range(2):
     inner_list = []
     for j in range(3):
           inner_list.append(0)
     outer_list.append(inner_list)
print(outer_list)

# now just try to change any element of the above outer list
outer_list[0][1] = 2

print(outer_list)

In the above code we are creating a new inner list every time we loop. This solves our initial problem.
if you execute above code this is what we will get

[[0, 2, 0], [0, 0, 0]]

Change the second according to above example code and then you will not get the error

Thanks for the explanation…