How To Create A 2d List With All Same Values But Can Alter Multiple Elements Within? (python)
I'm trying to create a list that holds this exact format: [[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]] and when list[3][0] = 9 is called, the list becomes [[2],[9],[2],[9],[2],[9],[2]
Solution 1:
>>> L = [[2], [2]] * 5
>>> L
[[2], [2], [2], [2], [2], [2], [2], [2], [2], [2]]
>>> L[3][0] = 9
>>> L
[[2], [9], [2], [9], [2], [9], [2], [9], [2], [9]]
This is because the same two lists are repeated over and over
>>>[id(x) for x in L]
[140053788793352, 140053788793288, 140053788793352, 140053788793288, 140053788793352, 140053788793288, 140053788793352, 140053788793288, 140053788793352, 140053788793288]
Post a Comment for "How To Create A 2d List With All Same Values But Can Alter Multiple Elements Within? (python)"