Concepts / Understanding Return Values and None

Understanding Return Values and None

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

  • Programming

A Two-Part Result

A method call can affect an object and also produce a return value. With sort(), these two results must be kept separate: the list is rearranged in ascending order, while the method returns None. Understanding that difference prevents a particularly damaging assignment mistake.

What do you think happens?

After calling sort() on a list, what should you expect?

  • The list is arranged in ascending order and the method returns None
  • The list is unchanged and the method returns a newly sorted list
  • The list is replaced by a newly sorted list automatically
Reveal answer

Answer: The list is arranged in ascending order and the method returns None

sort() modifies the list in place. It does not return a new sorted value; its return value is None.

Sorting In Place

The sort() method arranges the elements of a list in ascending order by modifying that list in place. In place means that the original list is changed directly. The list does not need to be replaced by a separate sorted result for its elements to become ordered.

callrearranges[3, 1, 2]before sort()sort()modifies the list[1, 2, 3]after sort()
What does the list contain before and after calling sort(), and where does the change occur?
python
Output
numbers is now [1, 2, 3].
repositionedrepositionedrepositioned3position 01position 01position 12position 12position 23position 2
How do the element positions change as sort() rearranges the list into ascending order?

The Meaning of None

A void method modifies an object and returns None rather than returning a new value. For sort(), the list modification is the useful effect, and None is the method's return value.

object being modifiedreturnsnumbers[1, 2, 3]sort()changes the listNonereturn value
Where does the sorted data go when sort() modifies the original list, and what value does the method return?

This gives sort() two observable outcomes. The list's elements are now in ascending order, and evaluating the method call produces None. The returned None is not the sorted list; the sorted elements are already in the list that was modified.

Tracing a Void Method

Suppose numbers starts as [3, 1, 2]. What are the list contents after numbers.sort(), and what value does the method call return?

Before the call: The list contains [3, 1, 2].

Apply sort(): sort() rearranges the elements of the list into ascending order.

Read the method result: Because sort() is a void method, its return value is None.

The list contains [1, 2, 3], and the method-call expression has the value None.

separate outcomesnumbers[1, 2, 3]Nonesort() result
After calling sort(), what are the list's elements and what value is stored in the method-call expression?

The Assignment Trap

numbers.sort()numbers = numbers.sort()numbers[3, 1, 2]numbers[1, 2, 3]numbers[3, 1, 2]numbersNone
What happens to the variable after writing numbers = numbers.sort(), and why does it become None?
  • Assigning the result of sort() back to the same variable.

    sort() modifies the list but returns None. The assignment therefore overwrites the variable with None.

    Fix: Call the method without assignment: numbers.sort().

  • Expecting the method-call expression to be the sorted list.

    The sorted elements remain in numbers, while the return value of sort() is None.

    Fix: Use numbers.sort() when you want to modify numbers directly, and do not assign its return value.

Practice the Trace

EASY

A list named values contains [4, 2, 3]. Trace the statement values.sort(). State the final contents of values and the return value of the method call. Then explain why values = values.sort() is not the correct replacement.

Hints
  • Separate the list's changed state from the method's return value.
  • sort() arranges the elements in ascending order.
  • The method returns None, so assigning its result changes the variable.

What do you think happens?

After values.sort(), what are the two results to record?

  • values is [2, 3, 4], and the method result is None
  • values is None, and the method result is [2, 3, 4]
  • values remains [4, 2, 3], and the method result is [2, 3, 4]
Reveal answer

Answer: values is [2, 3, 4], and the method result is None

sort() changes the list in place and returns None. Assignment is the operation that would overwrite the variable with that None value.

Key Takeaways

  1. sort() arranges list elements in ascending order.
  2. sort() modifies the original list in place.
  3. A void method returns None rather than a new value.
  4. Call sort() without assignment when you want to modify the list.
  5. Writing t = t.sort() overwrites t with None.

Key Takeaways

  • sort() rearranges a list into ascending order by modifying the list in place.
  • The list changes, but the return value of sort() is None.
  • Void methods should be called without assigning their return value.
  • The expression t = t.sort() overwrites the variable with None.