List Methods and Modifications
Aliasing occurs when two or more variables refer to the same object in memory. The assignment b = a creates an alias, not a copy.
One List, Two Names
Suppose a list is stored under the name a, and then you write b = a. It is tempting to think that b receives its own separate list. In Python, that assignment creates an alias instead: a and b refer to the same list object. This matters because lists are mutable. A modification made through b can therefore be seen through a as well.
["red", "blue"]
["red", "blue"]Testing Object Identity
Use the is operator when you want to know whether two variables refer to the exact same object. The expression a is b returns True when both names refer to one object and False when they refer to different objects. This is different from ==. The equality operator compares values, so two separate lists can contain equal values without being the same object.
a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) print(a == c) print(a is c)
Tracing a Shared Modification
What do you think happens?
After these assignments, what will a contain when it is printed? a = ["start"] b = a b.append("added") print(a)
Reveal answer
Answer: ["start", "added"]
b and a refer to the same list. The modification made through b changes that one shared list, so the new value is visible through a.
["start", "added"]
["start", "added"]
TrueThe important point is not which variable was used to make the modification. There is only one list object in this example. Since both names refer to it, both names show the changed contents afterward. This is why aliasing can make a list appear to change unexpectedly.
Making an Independent Copy
If two variables must hold separate lists, create a copy explicitly. The source identifies two ways to do this: b = a[:] and b = list(a). These operations create an independent list. Afterward, modifying one list does not affect the other.
Alias or Copy?
Compare b = a with b = a[:] when the list is later modified through b.
Create an alias: With a = ["first"] followed by b = a, both names refer to the same list object.
Modify the alias: A modification through b changes the shared list, so the changed contents are also visible through a.
Create a copy: With a = ["first"] followed by b = a[:], b refers to an independent copy of the list.
Modify the copy: A modification through b changes the copied list without changing the original list referred to by a.
b = a creates an alias. b = a[:] creates an independent list. The list(a) constructor is another source-supported way to create an independent copy.
["first"]
["first", "second"]
FalseFinding the Source of an Unexpected Change
When a list changes unexpectedly, first identify every variable that may refer to it. Then use is to test suspected pairs. If a is b is True, a modification through either name affects the same list. If you need the variables to change independently, replace the aliasing assignment with a copy such as a[:] or list(a).
items = ["notebook"] backup = items backup.append("pen") print(items) print(items is backup)
Common Aliasing Mistakes
Assuming b = a creates an independent list
The assignment gives both names a reference to the same list object.
Fix:
Use b = a[:] or b = list(a) when b must refer to an independent copy.Using == when the question is whether two variables are aliases
The == operator tests equality of values, not whether the objects are identical.
Fix:
Use a is b to test whether both variables refer to the same object.Looking only at the variable used for the modification
If backup and items are aliases, the modification changes the one shared list and is visible through items.
Fix:
Check suspected aliases with is and copy the list when independent changes are required.
Practice the Trace
Predict the three printed results before checking them: a = ["A"] b = a c = list(a) b.append("B") print(a) print(c) print(a is c)
Hints
- Determine which names refer to the original list.
- Determine which assignment creates an independent copy.
- Follow the modification through b, then compare a with c.
["A", "B"]
["A"]
FalseKey Takeaways
- b = a creates an alias: both names refer to the same list object.
- Use is to test object identity and == to test equality of values.
- Because lists are mutable, a modification through one alias is visible through every other alias.
- Use a[:] or list(a) to create an independent list copy.
- When a list changes unexpectedly, trace assignments and use is to locate unintended aliasing.
Key Takeaways
- An assignment such as b = a creates two names for one mutable list rather than two independent lists.
- The is operator tests whether variables refer to the same object; == tests whether their values are equal.
- Mutating a shared list through one variable changes what all aliases show.
- Use a[:] or list(a) when you need an independent list.
- Unexpected list changes can be debugged by tracing assignments and checking object identity.