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.
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.
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().
- 1Starting state
The list contains 8, 3, and 5, so its elements are not arranged in ascending order.
- 2Method call
Calling
sort()asks the list to arrange its elements in ascending order. The operation modifies the list in place. - 3Final 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]?
Reveal 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.
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 pattern | Effect on the original list | Value returned by the method | Correct variable handling |
|---|---|---|---|
| Void method such as sort() | Modifies the list in place | None | Call it without assignment |
| Method that returns a new value | The source pack does not specify its list behavior | A new value may be returned | Store 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.
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.
t = [3, 1, 2]
t = t.sort()The sort operation modifies the list, then assignment stores the method's returned None value in t.
t = [3, 1, 2]
t.sort()The sort operation modifies t directly, with no assignment of the returned value.
Common mistake Writing t = t.sort()
What it looks likeThe method call changes the list, but the assignment stores None in t.
Why it failsVoid methods return None rather than a new value.
Do this insteadWrite t.sort() without assignment.Common mistake Assuming that a changed list must also be returned as a new value
What it looks likeExpecting sort() to produce a separate sorted list value.
Why it failsThe source defines
sort()as modifying the list in place.Do this insteadTrack the list's changed state separately from the method's returned value.
Applying the Prediction Pattern
- 1Write down the list's contents before the method call.
- 2Ask whether the method modifies the list in place or returns a new value.
- 3If it is a void method, expect the object to change and the method result to be None.
- 4Do not assign the void method's return value back to the list variable.
- 5Write down the list's contents after the operation.
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.
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. - 3Void methods modify an object and return None.
- 4Call a void method without assignment:
t.sort(). - 5Writing 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.