Concepts / List Methods: append, remove, and reverse

List Methods: append, remove, and reverse

When a void method modifies an object, call the method without assigning its return value back to the variable.

  • Programming

The Method Call That Changes the List

A list method can change the list itself without producing a new list value for you to store. The source pack uses sort() as the concrete example: sort() arranges list elements in ascending order by modifying the list in place. This means the important result is the changed list, not a separate value returned from the method.

Key point

When a void method modifies an object, call the method without assigning its return value back to the variable.

list[3, 1, 2]sort()ascending orderlist[1, 2, 3]callmodifies
What changes when sort() is called on an unsorted list?

Tracing the List State

Worked example

Following a Sort Operation

A list variable named scores refers to the elements [8, 3, 5]. Predict its state after calling scores.sort().

  1. 1
    Starting state

    The list contains 8, 3, and 5, so its elements are not arranged in ascending order.

  2. 2
    Method call

    Calling sort() asks the list to arrange its elements in ascending order. The operation modifies the list in place.

  3. 3
    Final state

    The list now contains the same elements arranged from smallest to largest: [3, 5, 8].

Result

The list variable refers to a list whose elements are arranged in ascending order.

What do you think happens?

What is the list state after calling values.sort() when values begins as [4, 2, 6]?

  • The list becomes [2, 4, 6].

  • The list remains [4, 2, 6].

  • The list is replaced by a new unsorted list.

Reveal answer
Answer:

The list becomes [2, 4, 6].

The source defines sort() as an operation that arranges list elements in ascending order by modifying the list in place.

4position 02position 02position 14position 16position 26position 2movesmovesstays at position
How do element positions change as an unsorted list becomes ordered from smallest to largest?

Void Methods and Returned Values

A void method modifies an object and returns None rather than returning a new value. For a void method, two ideas must be kept separate: the object may have changed, while the method call itself does not provide a new value to store. In the source pack, sort() is the example of this pattern because it changes the list in place.

Operation patternEffect on the original listValue returned by the methodCorrect variable handling
Void method such as sort()Modifies the list in placeNoneCall it without assignment
Method that returns a new valueThe source pack does not specify its list behaviorA new value may be returnedStore the returned value only when the method is documented to return one

The source pack establishes the first row and contrasts it with the general idea of methods that return new values.

sort()modifies list in placelistchanged contentsnew valuestored separatelyNonereturned valuereturning methodnew valuemodifiesreturnsreturns
What changes in the original list, and what value does a void method return?

The Assignment Trap

The critical mistake is assigning the result of a void method back to the variable that holds the list. The source pack gives the pattern t = t.sort(). sort() modifies t, but the method returns None. Assignment then replaces the variable's previous value with that returned None value.

Before
t = [3, 1, 2]
t = t.sort()

The sort operation modifies the list, then assignment stores the method's returned None value in t.

After
t = [3, 1, 2]
t.sort()

The sort operation modifies t directly, with no assignment of the returned value.

my_listlist before sortingsort()modifies in placeNonereturned valuesorted_listreceives Nonecallsreturnsassigned to
What happens to a variable after writing sorted_list = my_list.sort()?
  • Common mistake

    Writing t = t.sort()

    What it looks like

    The method call changes the list, but the assignment stores None in t.

    Why it fails

    Void methods return None rather than a new value.

    Do this instead
    Write t.sort() without assignment.
  • Common mistake

    Assuming that a changed list must also be returned as a new value

    What it looks like

    Expecting sort() to produce a separate sorted list value.

    Why it fails

    The source defines sort() as modifying the list in place.

    Do this instead

    Track the list's changed state separately from the method's returned value.

Applying the Prediction Pattern

  1. 1
    Write down the list's contents before the method call.
  2. 2
    Ask whether the method modifies the list in place or returns a new value.
  3. 3
    If it is a void method, expect the object to change and the method result to be None.
  4. 4
    Do not assign the void method's return value back to the list variable.
  5. 5
    Write down the list's contents after the operation.
EASY

A list named items contains [7, 2, 5]. Predict both the list's contents and the method result after calling items.sort(). Then explain what would go wrong if the call were assigned back to items.

Hints
  • sort() arranges elements in ascending order.
  • sort() modifies the list in place.
  • A void method returns None.
MEDIUM

Compare these two actions conceptually: calling a void method without assignment and assigning its return value to a variable. State what happens to the original object and what value is stored by the assignment.

Hints
  • The source pack distinguishes modifying an object from returning a new value.
  • For a void method, the returned value is None.

Key Takeaways

  1. 1
    sort() arranges list elements in ascending order.
  2. 2
    sort() modifies the list in place rather than producing a new list value.
  3. 3
    Void methods modify an object and return None.
  4. 4
    Call a void method without assignment: t.sort().
  5. 5
    Writing t = t.sort() overwrites t with None after the list modification.

At a glance

  • sort() arranges list elements in ascending order by modifying the list in place.
  • A void method changes an object but returns None rather than a new value.
  • Calling t.sort() preserves the variable while changing the list.
  • Writing t = t.sort() assigns None to t and is the critical mistake to avoid.
  • To predict the result, track the list's contents and the method's returned value separately.

Related concepts

More concepts starting with L