I'm working through lists and variables in my intro course and ran into something I don't understand. I thought assigning a list to a new variable would make a separate copy, but changing one of them seems to change both.
Here's a minimal version:
original = [1, 2, 3]
copy_of_it = original
copy_of_it.append(4)
print("original:", original)
print("copy_of_it:", copy_of_it)
print(original is copy_of_it)
Output:
original: [1, 2, 3, 4]
copy_of_it: [1, 2, 3, 4]
True
I expected original to stay [1, 2, 3] since I only added 4 to copy_of_it. I've printed id() of both variables and they match, and I skimmed the docs section on mutable objects, but I still don't get why they're linked. Could someone explain what's happening under the hood?