List Methods: append, remove, and reverse
The sort() method arranges list elements in ascending order by modifying the list in place.
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().
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]?
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.
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 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.
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
- Write down the list's contents before the method call.
- Ask whether the method modifies the list in place or returns a new value.
- If it is a void method, expect the object to change and the method result to be None.
- Do not assign the void method's return value back to the list variable.
- Write 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
- sort() arranges list elements in ascending order.
- sort() modifies the list in place rather than producing a new list value.
- Void methods modify an object and return None.
- Call a void method without assignment: t.sort().
- 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.