So, I was working on Python for making List of arrays for my project work.
Sample code:
var0 = 0
var1 = 0
car = [0,0]
listitem = [car[:] for i in range(10)]
for i in range(10):
car[0] = var0 + i + 1
car[1] = var1 + i
listitem[i] = car
print listitem
As the logic would suggest we would be expecting an output like:
[[1,0],[2,1],[3,2],[4,3],[5,4],[6,5],[7,6],[8,7],[9,8],[10,9]]
But the output comes as:
[[10,9],[10,9],[10,9],[10,9],[10,9],[10,9],[10,9],[10,9],[10,9],[10,9]]
I have come with a theory where the car array uses some pointers to the variable space.
Does anyone has a probable explanation for the same? Thank you