print liveCoords # displays [(0,0),(1,0),(2,0)]
if population > 3 or population < 2:
if (j,i) in liveCoords:
try:
del liveCoords2[liveCoords2.index((j,i))]
except:
pass
elif population == 3:
if (j,i) in liveCoords:
pass
else:
liveCoords2.append((j,i))
print liveCoords # displays [(1,0),(2,0)]
My question is not about how to do something specific, it is how my list liveCoords could possible be changed between those 2 points, from what I can see, nothing is being done that could possibly alter liveCoords, yet it is being altered.
Did you create
liveCoords2with the lineliveCoords2 = liveCoords? If so then they are both references to the same list, so adding or removing elements inliveCoords2would affectliveCoordsas well.If this is the case, then changing the assignment to
liveCoords2 = list(liveCoords)orliveCoords2 = liveCoords[:]would fix this. Note that both of these methods will create a shallow copy, if any elements inlistCoordsare mutable objects thenlistCoords2will have references to the same objects aslistCoords, this should be fine for your current code but if you do end up needing to create a deep copy you can use the copy module: