In this article, you will learn everything about the Python list sort method to sort the Python list. Python lists provide a sort() method capable of sorting the list in ascending order and descending order.
Headings of Contents
Interdoction of Python List Sort Method
The list sort()
method is capable of sorting the list in ascending order by default. You can also specify the function to decide the function criteria.
Syntax
This is the basic syntax of the Python List sort function.
list.sort(reverse=True|False, key=myFunc)
Parameters
There are two parameters available in the list sort method.
- reverse:- Optional, reverse=True will sort the list in descending order.Default is False.
- key:- Optional, A function to specify the sorting criteria.
Python List sort() Method Examples
Here we are going to sort a sample Python list in ascending order and descending order.
Example: Sort the Python list in ascending order
In this example, we are going to sort the list in ascending order.
lst = ['Apple', 'Grapse', 'Orange', 'Banana', 'Appricot', 'Cherry']
# sort the list in asending order
lst.sort()
print(lst)
Output
['Apple', 'Appricot', 'Banana', 'Cherry', 'Grapse', 'Orange']
Example: Sort the Python list in descending order
In this example, we are going to sort the list in descending order by adding the reverse=True parameter.
lst = ['Apple', 'Grapse', 'Orange', 'Banana', 'Appricot', 'Cherry']
# sort the list in descending order
lst.sort(reverse=True)
print(lst)
Output
['Orange', 'Grapse', 'Cherry', 'Banana', 'Appricot', 'Apple']
Conclusion
So, In this article, we have seen all about the Python list sort method with the help of examples. If you have a large list of items, then is one of the best methods to sort the list in ascending order and descending order as per your requirement.
I hope you have understood this article well. If you like this article, please visit for further Python tutorials.
Sort() Method Reference:- Click Here
Thanks for your valuable time. 👏👏👏