Please take a look at the following code:
import numpy as np
list1 = [1,2,3]
list2 = list1
list2.append(4)
print list2
print list1
The result is [1,2,3,4] for both prints. I expected to have [1,2,3,4] for list2 and [1,2,3] for list1 since I only appended '4' to list2. It looks like the assignment list2 = list1 transferred the instruction on list2 to list1.
First, I'd like to understand why that is and second, is there a way to make a variable B that is identical to variable A but such that any instruction on B (a re-assignment for example), will modify B but not A?
Obviously, I could do
list1 = [1,2,3]
list2 = [1,2,3]
list2.append(4)
and get what I want. But what if list1 is say, a (numpy) 100x100 array? Then I'd rather have a way to 'copy' list1 to another variable than manually rewriting it.
I am not an experienced programmer and am fairly new to Python. I've also tried to formulate this question as simply as I could and have not found anything that directly answered it on this site. Please redirect me to anything I might have missed and accept my apologies in advance. Thanks!