Search⌘ K
AI Features

Sorting and Reversing

Explore how to manipulate Python lists by sorting and reversing them using list methods, built-in functions like sorted and reversed, and slicing operations. Understand the differences between in-place modifications and returning new lists to manage list data efficiently.

Using list methods

Let’s learn how to reverse and sort lists.

Python 3.8
lst = [10, 2, 0, 50, 4]
lst.reverse( )
print(lst) # prints [4, 50, 0, 2, 10]
lst.sort( )
print(lst) # prints [0, 2, 4, 10, 50]
lst.sort(reverse = True) # sort items in reverse order
print(lst) # prints [50, 10, 4, 2, 0]

Note: The reverse() ...