Because they are NOT the same. The second one (array2) is logically a 2 dimensional 4x4 array of integers.
In the first one (array) you created a list of 4 integer (the inner part), then you repeated that list 4 times. Logically speaking you get a one dimensional list, containing 4 references to the SAME list of 4 integers. That means that when you change an element in the inner list, you get it changed in each row, because each row points to the same object (list).
If you change the first element of each row you'll get an apparently odder behavior, because you'll see only the last value you put, in each row, because you are overwriting the same object.
5
u/pdepmcp Aug 27 '20
Because they are NOT the same. The second one (array2) is logically a 2 dimensional 4x4 array of integers.
In the first one (array) you created a list of 4 integer (the inner part), then you repeated that list 4 times. Logically speaking you get a one dimensional list, containing 4 references to the SAME list of 4 integers. That means that when you change an element in the inner list, you get it changed in each row, because each row points to the same object (list).
If you change the first element of each row you'll get an apparently odder behavior, because you'll see only the last value you put, in each row, because you are overwriting the same object.