Is there any way to define 2-d array in python? [duplicate]

  arrays, python

Trying a way to define a 2-d array in python as similar to 1-d array but in 2-d array can’t get the element assign to the mentioned index properly.

pas = [[0]*7]*7 #2-d array
pas[0][0] = 1 # assign 1 to the 1st column of the 1st row
pas[1][1] = 1 # assign 1 to the 2nd column of the 2nd row
print(pas)

I wish it should have been like this but solution comes different.

Solution:-
[[1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0]]

Although the result comes out is 2-d array but the all the other index which were not altered except [0][0] and [1][1] have also been changed. Can someone explain me why?
And if there is better way to create a 2-d array in python can somebody also elaborate it.

Source: Python Questions

LEAVE A COMMENT