Concepts / List Methods and Modifications

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.

  • Programming

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.

python
Output (expected)
["red", "blue"]
["red", "blue"]
refers torefers toavariable name["red", "blue"]one list objectbvariable name
What does memory look like after b = a, and how can both names refer to one list?

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)

isisis== Truealist [1, 2, 3]list [1, 2, 3]shared objectbsame list as alist [1, 2, 3]separate objectcseparate list [1, 2, 3]
How can two lists have equal values without being the same object?

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)

  • ["start"]
  • ["added"]
  • ["start", "added"]
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.

python
Output (expected)
["start", "added"]
["start", "added"]
True

The 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.

refers torefers torefers torefers toa["start"]["start"]shared lista["start", "added"]["start", "added"]same shared listb["start"]b["start", "added"]
What changes when b modifies the shared list, and what does a contain afterward?

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.

python
Output (expected)
["first"]
["first", "second"]
False
refers torefers torefers torefers toashared list["first"]one objectaoriginal list["first"]one objectbsame listbcopied list["first"]different object
How are references and list contents different after b = a compared with b = a[:]?

Finding 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)

refers torefers toused forchangesnow showsitems["notebook"]["notebook"]shared list["notebook", "pen"]visible through itemsbackupcreated by backup = itemsappend("pen")modification
How does a modification flow from backup to items when both names refer to one list?

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

MEDIUM

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.
Output
["A", "B"]
["A"]
False

Key Takeaways

  1. b = a creates an alias: both names refer to the same list object.
  2. Use is to test object identity and == to test equality of values.
  3. Because lists are mutable, a modification through one alias is visible through every other alias.
  4. Use a[:] or list(a) to create an independent list copy.
  5. 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.