Concepts / List Methods and Modifications

List Methods and Modifications

Suppose a list is stored under the name a, and then you write b = a.

  • Programming
Interactive lab

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

  1. name = object binds a name; b = a makes b point at the same object.
  2. Mutating a list (append, +=) changes the one shared object.
  3. a = a + [x] and y = y + 1 create NEW objects and re-point one name.
  4. 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.

Educational simulation

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.

python
Output (expected)
["red", "blue"]
["red", "blue"]
avariable name["red", "blue"]one list objectbvariable namerefers torefers to
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.

Code walkthrough
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)
print(a == c)
print(a is c)
  1. 1
    The assignment b = a makes b refer to the object already referred to by a.
  2. 2
    The separate list expression assigned to c creates a different list object with equal contents.
  3. 3
    a is b is True because the two names refer to the same object.
  4. 4
    a == c is True because the list values are equal.
  5. 5
    a 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.
Output
True
True
False
alist [1, 2, 3]list [1, 2, 3]shared objectbsame list as alist [1, 2, 3]separate objectcseparate list [1, 2, 3]isisis== True
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.

a["start"]["start"]shared lista["start", "added"]["start", "added"]same shared listb["start"]b["start", "added"]refers torefers torefers torefers to
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.

Worked example

Alias or Copy?

Compare b = a with b = a[:] when the list is later modified through b.

  1. 1
    Create an alias

    With a = ["first"] followed by b = a, both names refer to the same list object.

  2. 2
    Modify the alias

    A modification through b changes the shared list, so the changed contents are also visible through a.

  3. 3
    Create a copy

    With a = ["first"] followed by b = a[:], b refers to an independent copy of the list.

  4. 4
    Modify the copy

    A modification through b changes the copied list without changing the original list referred to by a.

Result

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
ashared list["first"]one objectaoriginal list["first"]one objectbsame listbcopied list["first"]different objectrefers torefers torefers torefers to
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).

Code walkthrough
items = ["notebook"]
backup = items

backup.append("pen")

print(items)
print(items is backup)
  1. 1
    Start with one list referred to by items.
  2. 2
    Assigning backup = items gives the same list a second name.
  3. 3
    The modification through backup changes the shared list.
  4. 4
    Print items to observe the unexpected change.
  5. 5
    Use 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.
Output
["notebook", "pen"]
True
items["notebook"]["notebook"]shared list["notebook", "pen"]visible through itemsbackupcreated by backup = itemsappend("pen")modificationrefers torefers toused forchangesnow shows
How does a modification flow from backup to items when both names refer to one list?

Common Aliasing Mistakes

  • Common mistake

    Assuming b = a creates an independent list

    What it looks like
    a = [1, 2]
    b = a
    Why it fails

    The assignment gives both names a reference to the same list object.

    Do this instead

    Use 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 like
    a == b
    Why it fails

    The == operator tests equality of values, not whether the objects are identical.

    Do this instead

    Use 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 like
    backup.append("pen")
    Why it fails

    If backup and items are aliases, the modification changes the one shared list and is visible through items.

    Do this instead

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

Related concepts

More concepts starting with L