Assigning using double indexing in Python

Viewed 12

When I create a 2d list in the below-given manner and assign an element using double indexing, like

l = [[-1, -1], [-1, -1], [-1, -1]]
l[2][0] = 0
print(l)
#output: [[-1, -1], [-1, -1], [0, -1]]

The above output is expected but,

When I create the same 2d list using the * operator and try to assign an element using double indexing, like

l = [[-1, -1]] * 3
l[2][0] = 0
print(l)
#output: [[0, -1], [0, -1], [0, -1]]

The above output is not expected. Can someone explain why the output differs even when the lists are identical as well as the assignment done using double indexing is the same?

0 Answers
Related