0

While solving a problem, I've come across this phenomenon and found the result kind of weird somehow.

Problem: Given a list D containing many lists, such as

D = [[0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0]] (6 rows, 6 columns).

I want to change the first list of D into [0, 1, 2, 3, 4 ,5] so the resulting D is supposed to be

D = [[0,1,2,3,4,5], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0]]

But when I used this expression D[0][0] = 1 (because I'd like to do a for loop through D[0] and change every element of D[0]), the list D will be

D = [[1,0,0,0,0,0], [1,0,0,0,0,0], [1,0,0,0,0,0], [1,0,0,0,0,0], [1,0,0,0,0,0], [1,0,0,0,0,0]]

Question: I don't know what was happening there, can anyone explain to me what's happend?

1 Answers1

0

Try

D[0] = [0, 1, 2, 3, 4 ,5]

output

>>> D = [[0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0]]
>>> D[0]
[0, 0, 0, 0, 0, 0]
>>> D[0] = [0, 1, 2, 3, 4 ,5]
>>> D
[[0, 1, 2, 3, 4, 5], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>>