Concepts / Understanding Mutability

Understanding Mutability

Always trace the list and the return value separately. A method can change the list in place while its return value is None.

  • Programming

The List and the Return Value

When you call a list method, two questions matter: what happens to the list itself, and what value does the method call produce? Python's append, extend, and sort methods modify the existing list in place. Each one returns None. This means the list can change even though the method call does not produce a new list value.

Key point

Always trace the list and the return value separately. A method can change the list in place while its return value is None.

Tracing an In-Place Change

What do you think happens?

Suppose values starts as [3, 1]. After values.append(5), what are the list and the return value?

  • The list is [3, 1], and the return value is [3, 1, 5].

  • The list is [3, 1, 5], and the return value is None.

  • The list is None, and the return value is [3, 1, 5].

Reveal answer
Answer:

The list is [3, 1, 5], and the return value is None.

append modifies the existing list in place and returns None. The changed list and the method's return value are separate results.

values[3, 1]append(5)in placevalues[3, 1, 5]Nonemethod return valuecallchanges listreturns
What changes when append is called, and what value does the method call return?

The diagram separates two outcomes that are easy to mix up. The list changes from [3, 1] to [3, 1, 5]. The call to append(5), however, evaluates to None. The changed list is the object you continue using; None is the method's return value.

Three Ways to Change Contents

MethodEffect on the listReturn value
appendAdds one item to the listNone
extendAdds multiple elements to the listNone
sortReorders the existing list's contentsNone

append, extend, and sort all modify the list in place, but their operations are different.

appendadds one itemone itemextendadds multiple elementsmultiple elementssortreorders contentsreordered listperformsperformsperforms
How does each method change a list's contents: adding one item, adding multiple elements, or reordering existing contents?

append and extend both add elements, but they treat their arguments differently. append adds one item to the list. extend adds multiple elements to the list. This distinction is especially important with nested data or when combining lists. sort does not add elements; it changes the order of the existing list contents. All three methods modify the original list and return None.

Method Calls in Practice

Code walkthrough
numbers = [3, 1]
append_result = numbers.append(5)
print(numbers)
print(append_result)

parts = [1, 2]
extend_result = parts.extend([3, 4])
print(parts)
print(extend_result)

scores = [4, 2, 3]
sort_result = scores.sort()
print(scores)
print(sort_result)
  1. 1
    numbers starts as [3, 1]. append(5) changes it to [3, 1, 5], while append_result receives None.
  2. 2
    parts starts as [1, 2]. extend([3, 4]) changes it to [1, 2, 3, 4], while extend_result receives None.
  3. 3
    scores starts as [4, 2, 3]. sort() changes the order to [2, 3, 4], while sort_result receives None.
Output
[3, 1, 5]
None
[1, 2, 3, 4]
None
[2, 3, 4]
None
Key point

The assignment statements capture None, not a replacement list. The lists themselves have already been modified before the return values are assigned.

The Assignment Trap

items[2, 1]items.sort()reorders listitems[1, 2]items = Noneresult assignedNonevariable valuecallmodifies listreturns Noneoverwrites variable
What happens when code assigns the result of a method that changes a list in place and returns None?
  • Common mistake

    Assigning the result of an in-place method back to the list variable

    What it looks like
    items = items.sort()
    Why it fails

    sort changes the list in place and returns None, so the assignment replaces the value of items with None.

    Do this instead

    Call items.sort() without assigning its result.

  • Common mistake

    Treating append and extend as interchangeable

    What it looks like

    Using append when the intention is to add multiple elements

    Why it fails

    append and extend both add elements, but they treat the argument differently: append adds one item, while extend adds multiple elements.

    Do this instead

    Choose append for one item and extend for multiple elements.

  • Common mistake

    Checking only the return value and ignoring the changed list

    What it looks like

    Assuming that a None return value means the list did not change

    Why it fails

    These methods modify the list in place even though they return None.

    Do this instead

    Trace the list after the call and separately record the method's return value.

Predict Before Running

MEDIUM

For each call, predict both the final list and the method's return value.

1. values = [5, 2]
   result = values.append(8)
2. values = [5, 2]
   result = values.extend([8, 9])
3. values = [5, 2]
   result = values.sort()

Then explain why assigning values = values.sort() causes a problem.

Hints
  • All three methods modify the existing list in place.
  • All three methods return None.
  • append adds one item, extend adds multiple elements, and sort reorders the existing contents.
Worked example

Tracing extend

Determine the list and return value after values = [5, 2] followed by result = values.extend([8, 9]).

  1. 1
    Start

    The list is [5, 2].

  2. 2
    Apply the method

    extend adds multiple elements to the existing list, so the list becomes [5, 2, 8, 9].

  3. 3
    Record the return value

    extend modifies the list in place and returns None, so result is None.

Result

values is [5, 2, 8, 9], and result is None.

Working Rule

  1. 1
    append, extend, and sort modify a list in place.
  2. 2
    append adds one item, extend adds multiple elements, and sort reorders the existing list contents.
  3. 3
    Each of these methods returns None.
  4. 4
    Do not assign the result of one of these methods back to the list variable, because that assignment overwrites the variable with None.
  5. 5
    When debugging, trace the list's state and the method's return value as two separate outcomes.

Key Takeaways

  • append, extend, and sort all change the existing list rather than producing a new list value.
  • append adds one item, extend adds multiple elements, and sort changes the order of existing contents.
  • The return value of each method is None.
  • Calling a method without assignment preserves the changed list; assigning its result back replaces the variable with None.

More concepts starting with U