Concepts / List Methods: append, remove, and reverse

List Methods: append, remove, and reverse

The sort() method arranges list elements in ascending order by modifying the list in place.

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

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

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

Tracing the List State

Following a Sort Operation

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

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

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

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

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.

movesmovesstays at position4position 02position 02position 14position 16position 26position 2
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
modifiesreturnsreturnssort()modifies list in placelistchanged contentsnew valuestored separatelyNonereturned valuereturning methodnew value
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.

callsreturnsassigned tomy_listlist before sortingsort()modifies in placeNonereturned valuesorted_listreceives None
What happens to a variable after writing sorted_list = my_list.sort()?
  • Writing t = t.sort()

    Void methods return None rather than a new value.

    Fix: Write t.sort() without assignment.

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

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

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

Applying the Prediction Pattern

  1. Write down the list's contents before the method call.
  2. Ask whether the method modifies the list in place or returns a new value.
  3. If it is a void method, expect the object to change and the method result to be None.
  4. Do not assign the void method's return value back to the list variable.
  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. sort() arranges list elements in ascending order.
  2. sort() modifies the list in place rather than producing a new list value.
  3. Void methods modify an object and return None.
  4. Call a void method without assignment: t.sort().
  5. Writing t = t.sort() overwrites t with None after the list modification.

Key Takeaways

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