Goal: I want to create a list, make a copy of it, and manipulate the copy while not affecting the original.
I created a toy example to show the problem. When I copy the listand append the copy, somehow this traces back to the original. How can I keep this from affecting the original?
class testing:
def __init__(self):
self.array1 = [1, 2, 3, 4, 5]
self.array2 = self.array1
def display1(self):
print(self.array1)
def display2(self):
ary = self.array2
ary.append(6)
print(ary)
if __name__ == "__main__":
test = testing()
test.display1()
test.display2()
test.display1()
The result is:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
The third result shows that the first list has the appended '6' that was only added to the copy list