Kickstart 2020 with new opportunities! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
5. sort()
Sorting – sorted()
Sorting – reverse() and reversed()
Summary

Instruction

Let's take a look at the code again:

books = [
  'Sound of Steel',
  ...
]
books.sort()
print(books)

The command books.sort() was used to sort the books list alphabetically. After this operation, the books variable was permanently modified, and we got a sorted list. This kind of operation, which modifies the original data structure without any helper data structure, is called an in-place operation. sort() is an in-place function because it directly modifies a list and does not need additional memory (additional data structures) to do so.

Note that sort() only works for lists. Dictionaries and sets are unordered, so we can't sort them. In turn, tuples are immutable, so we can't modify them.

Exercise

The list in the template code presents students' exam results. Find and print the third-highest score.

Stuck? Here's a hint!

Sort the list with sort(), and print exam_results[-3].