Autumn Offers - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
Sorting – sorted()
9. sorted()
Sorting – reverse() and reversed()
Summary

Instruction

Very well done! So far, we've worked with the sort() function, which operates in place and modifies the original list. However, we sometimes want to avoid that, so we use a similar function named sorted():

sorted(books)

Unlike sort(), sorted() doesn't modify the original list; it returns a sorted copy. Let's check that in an exercise.

Exercise

Run the template code. Note that sorted() uses a slightly different syntax than sort().

As you can see, the original list in the books variable is left untouched. The sorted list is stored in a new variable named sorted_books.