Consider the following code:
listA should never change. But it does. It works the way I expected on a one-dimensional list, but on a two-dimensional list, listA changes. How do I avoid this, and (out of curiosity) why the hell doesn't python just assign the value rather than the reference anyway?
Code:
listA = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']] changeCoordinates = ((2, 3), (1, 3)) for coordinateList in changeCoordinates: listB = listA[:] x = coordinateList[0] y = coordinateList[1] listB[x][y] = 'foo' print "list A: %s" % listA print "list B: %s" % listB
Comment