Mutability and Aliasing
Two names pointing at one list, why changing one changes both, and the difference between rebinding a name and mutating an object.
Overview
Assignment does not copy
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4]
b = a attaches a second label to the same list. There is one list and two names for it, so a change through either name is visible through both. a is b is True, which is the test for "the same object" as opposed to ==, which asks about contents.
To get a second list, ask for one: a[:], list(a) or a.copy().
aliasing.py
mutable_arguments.py
Worth knowing
b = a gives the object a second name. It does not copy anything.is asks "the same object?"; == asks "the same contents?".[[0]*3]*3 repeats one inner list three times. Use a comprehension.