Skip to content Skip to sidebar Skip to footer

Unexpected Update Result On The Quickly Nested List In Python

Why couldn't the first element but the whole column be updated below? >>> x=2*[2*[1]] >>> x [[1, 1], [1, 1]] >>> x[0][0]=2 >>> x [[2, 1], [2, 1]

Solution 1:

Even tho this is a clear duplicate but use range:

>>>x=[[1for i inrange(2)] for x inrange(2)]>>>x
[[1, 1], [1, 1]]
>>>x[0][0]=2>>>x
[[2, 1], [1, 1]]
>>>

At least still able to do:

>>>x=[[1]*2for x inrange(2)]>>>x[0][0]=2>>>x
[[2, 1], [1, 1]]
>>>

Post a Comment for "Unexpected Update Result On The Quickly Nested List In Python"