Best April deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
Sorting – sorted()
10. sort() vs. sorted()
Sorting – reverse() and reversed()
Summary

Instruction

Let's compare sort() and sorted():

  1. sort() works in place. After invoking the function, the books variable is immediately modified.
    Syntax: books.sort().
  2. sorted() creates a sorted copy of the list. The original list is not modified, so remember to assign the result of sorted() to a variable if you want to reuse it!
    Syntax: sorted_books = sorted(books).

Exercise

Take a look at the template code. The get_positive_elements_sorted(input_list) function should accept a list with numbers, filter out non-positive numbers, sort the remaining positive numbers, and return a sorted copy of the list with positive numbers only. However, something is wrong – the elements are not sorted when returned from the function.

Investigate the problem, and fix the function.

Stuck? Here's a hint!

Note that sorted(list_to_return) returns a sorted copy of the list, but the function does nothing with the copy.