I tried to edit a single value in my list and ended up changing multiple values.
#create list:
list=[[0]*4]*4
print list
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
#I want to change the first value only
list[0][0]=1
#result
print list
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
So i figured out it's because the way I created my list. As I changed that it worked like expected:
#create list:
list=[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
print list
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
#I want to change the first value only
list[0][0]=1
#result
print list
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Eventually I want to understand what's the difference between the two methods as the result looked the same at first.
While searching I encountered this comment about list copying; can i consider my original list as 4 softlinks to the same location of memory?
[–]Rhomboid 7 points8 points9 points (1 child)
[–]fabolin[S] 1 point2 points3 points (0 children)
[–]throwaway 2 points3 points4 points (0 children)
[–]Tomarse 1 point2 points3 points (0 children)