I've got a problem where I use .append() to add coordinates, in the form of [x,y] to a list, then change the coords, add it again, and so on. But when I try to use the list for something, all the indexes where I added that coords have the current value of the coords, not the one I added.
I could paste the code but it's really large and uses Pygame, so I'll make an example of what happens.
Can anyone tell me is this is intended or if it's an issue, and how to get around it to have the first code work the same way as the second?
Thanks in advance.
I could paste the code but it's really large and uses Pygame, so I'll make an example of what happens.
a = [[0,0],2,[1,2]] b = [0,0] while b[0]<100: a.append(b) b[0] += 1 b[1] += 1 print(a)This should print increasing numbers in sets of two, but instead it prints [[0,0],2,[1,2],[100,100],[100,100],[100,100]...], so it appears that the values inside the list change with the coords. I thought that this was probably a feature of lists, that is, refreshing its values constantly, but then I wrote this
a = [5,2,6] b = 0 while b<100: a.append(b) b+=1 print(a)and this does print [5,2,6,0,1,2,3,4,5,6...]. So it appears to have something to do with .append()ing lists to lists.
Can anyone tell me is this is intended or if it's an issue, and how to get around it to have the first code work the same way as the second?
Thanks in advance.