# [python] 嵌套列表索引[重复]

``````In [151]: some_list = 4 * [(4 * [0])]

In [152]: [id(x) for x in some_list]
Out[152]: [148641452, 148641452, 148641452, 148641452]

In [160]: some_list[0][1]=5  #you think you changed the list at index 0 here

In [161]: some_list
Out[161]: [[0, 5, 0, 0], [0, 5, 0, 0], [0, 5, 0, 0], [0, 5, 0, 0]]  #but all lists are changed``````

``````In [156]: some_list=[[0]*4 for _ in range(4)]

In [157]: some_list
Out[157]: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

In [158]: [id(x) for x in some_list]
Out[158]: [148255436, 148695180, 148258380, 148255852]

In [163]: some_list[0][1]=5

In [164]: some_list
Out[164]: [[0, 5, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]  #works fine in this case``````
Question

``````some_list = 4 * [(4 * [0])]
for i in range(3):
for j in range(3):
some_list[i+1][j+1] = 1
for i in range(4):
print(some_list[i])``````

``````[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 1, 1]``````

``````[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 1, 1]``````