fbpx
1- Introduction
2- Python Basic
3- Learning Python
4- Python Literals
5- Arithmetic operators
6- Function INPUT() and STRING operations
7- Comparison Operators and Conditions
8- Loops in Python
9- Logic and bit operators
10- Lists and Arrays in Python
11- Functions
12- Tuples, Dictionaries
13- Conclusion
14- Practice Exams

L10.4 Sorting Lists

There are two methods to sort any list. sort() and reverse(). Both of these can work with numbers and strings.

Sort() method

You can use this method to sort a list.

Refer to code below and experiment with it.

lst = [5, 3, 1, 2, 4]
print(lst)

lst.sort()
print(lst)  # outputs: [1, 2, 3, 4, 5]

reverse() Method

You can use this method to reverse the list. Refer to code below and experiment with it. Expected output : [4, 2, 1, 3, 5]

lst = [5, 3, 1, 2, 4]
print(lst)

lst.reverse()
print(lst)  

0
Wanna ask a question or say something? Go aheadx
()
x
Scroll to Top