The way you are copying the list (both ways) is known as a shallow copy. This can be better seen this way:
l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]
originalList = [l1,l2,l3]
temp = originalList[:]
l1[0] = 0
>>> temp
[[0, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> originalList
[[0, 2, 3], [4, 5, 6], [7, 8, 9]]
When you make a shallow copy of originalList
, you are still getting references to l1
, l2
, and l3
. Any changes you make to those smaller lists will be reflected in both originalList
and any shallow copies you make. You want a deep copy operation. For your purposes, Scott Hunter's answer of
temp = [x[:] for x in originalList]
will work. In the general case, the method deepcopy from the copy
module should get you a deep copy of whatever objects you need. It is still likely better to use in this case as it is a few orders of magnitude faster.