List Methods and Modifications
Suppose a list is stored under the name a, and then you write b = a.
Try it: Names and Objects
How Python variables are names bound to objects: assignment never copies, mutating a list is seen through every name that points at it, and ints are replaced rather than changed.
How it works
- name = object binds a name; b = a makes b point at the same object.
- Mutating a list (append, +=) changes the one shared object.
- a = a + [x] and y = y + 1 create NEW objects and re-point one name.
- Passing a list to a function passes the reference, so the function can change it.
Default run (7 steps): Two names, one list. Every name is a label that points at an object. … Printed: [1, 2, 3] / True
Simplified: Six fixed scripts on a tiny heap; you choose the values. Object numbers are illustrative, not real id() values.
Loading the simulation…
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)- 1The assignment b = a makes b refer to the object already referred to by a.
- 2The separate list expression assigned to c creates a different list object with equal contents.
- 3a is b is True because the two names refer to the same object.
- 4a == c is True because the list values are equal.
- 5a is c is False because the objects are different.
- a and b are aliases for the same list object.
- a and c contain equal values.
- a and c are different list objects.
True
True
FalseTracing 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
["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.
- 1Create an alias
With a = ["first"] followed by b = a, both names refer to the same list object.
- 2Modify the alias
A modification through b changes the shared list, so the changed contents are also visible through a.
- 3Create a copy
With a = ["first"] followed by b = a[:], b refers to an independent copy of the list.
- 4Modify 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)- 1Start with one list referred to by items.
- 2Assigning backup = items gives the same list a second name.
- 3The modification through backup changes the shared list.
- 4Print items to observe the unexpected change.
- 5Use items is backup to confirm the alias.
- items is the original variable.
- backup = items creates an alias rather than an independent backup.
- The modification through backup is visible through items.
- The identity test confirms the two names refer to one object.
["notebook", "pen"]
TrueCommon Aliasing Mistakes
Common mistake Assuming b = a creates an independent list
What it looks likea = [1, 2] b = aWhy it failsThe assignment gives both names a reference to the same list object.
Do this insteadUse b = a[:] or b = list(a) when b must refer to an independent copy.
Common mistake Using == when the question is whether two variables are aliases
What it looks likea == bWhy it failsThe == operator tests equality of values, not whether the objects are identical.
Do this insteadUse a is b to test whether both variables refer to the same object.
Common mistake Looking only at the variable used for the modification
What it looks likebackup.append("pen")Why it failsIf backup and items are aliases, the modification changes the one shared list and is visible through items.
Do this insteadCheck 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
- 1b = a creates an alias: both names refer to the same list object.
- 2Use is to test object identity and == to test equality of values.
- 3Because lists are mutable, a modification through one alias is visible through every other alias.
- 4Use a[:] or
list(a)to create an independent list copy. - 5When a list changes unexpectedly, trace assignments and use is to locate unintended aliasing.
At a glance
- 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.