Python List Sorting and Reversing
Core idea
items.sort() orders the existing list; items.reverse() flips its current order without sorting. Both return None. sorted(items) builds a new sorted list, and reversed(items) provides a reverse iterator (an object that supplies items on demand); use list(reversed(items)) to collect them. reverse=True sorts comparable values in descending order.
times = [9, 3, 6]ordered = sorted(times)print(ordered)print(times)sorted creates a separate list; times still has its original order.
Sort descending in place
prices = [5, 2, 8, 5]print(prices.sort(reverse=True))print(prices)The list changes and duplicates remain. The return is None.
Reverse without sorting
items = [3, 1, 2]print(list(reversed(items)))items.reverse()print(items)Both yield the opposite order, not descending numeric order. Only reverse changes items.
Common mistake
Avoid assigning the result of sort to the list name. Also avoid sorting mixed numbers and strings together: they do not have a common ordering in Python 3.
Practice
Use [5, 1, 4] to compare sorted, sorted with reverse=True, and reversed converted to a list.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sorting and reversing
Check your understanding of list sorting and reversing.
What is printed?
items = [4, 1, 3]sorted(items)print(items)What is printed?
items = [2, 1]print(items.sort())What is printed?
items = [4, 7, 2]items.reverse()print(items)Which creates a new list sorted in descending order?