Home »
Python »
Python Programs
Python | Program to sort the elements of given list in Ascending and Descending Order
Sort a Python List: In this tutorial, we will learn how to sort the elements of a list in ascending and descending order in Python.
By IncludeHelp Last updated : June 22, 2023
Problem statement
Given a list of the elements and we have to sort the list in Ascending and the Descending order in Python.
Sort list elements in ascending and descending order
Soring list elements means arranging the elements of the list in either increasing order (ascending order) or decreasing order (descending order). Ascending or descending order sorting of the list can be done using the list.sort() method. To sort the list in ascending order, simply call the sort() method with this list. And, to sort the list in descending order, you have to pass `reverse = True` as an argument with the sort() method.
Sort a list in ascending order
As discussed, you can use the sort() method with this list without specifying any of the parameters. It will sort this list in ascending order.
Syntax
The syntax of the function to sort a list in ascending order is as follows:
list.sort()
Python program to sort a list in ascending order
# List of integers
num = [10, 30, 40, 20, 50]
# sorting and printing
num.sort()
print (num)
# List of float numbers
fnum = [10.23, 10.12, 20.45, 11.00, 0.1]
# sorting and printing
fnum.sort()
print (fnum)
# List of strings
str = ["Banana", "Cat", "Apple", "Dog", "Fish"]
# sorting and printing
str.sort()
print (str)
The output of the above program will be:
[10, 20, 30, 40, 50]
[0.1, 10.12, 10.23, 11.0, 20.45]
['Apple', 'Banana', 'Cat', 'Dog', 'Fish']
Sort a list in descending order
To sort a list in ascending order, you can use the sort() method with this list by specifying the reverse parameter as True. It will reverse the sorting.
Syntax
The syntax of the function to sort a list in descending order is as follows:
list.sort(reverse=True)
Python program to sort a list in descending order
# List of integers
num = [10, 30, 40, 20, 50]
# sorting and printing
num.sort(reverse=True)
print (num)
# List of float numbers
fnum = [10.23, 10.12, 20.45, 11.00, 0.1]
# sorting and printing
fnum.sort(reverse=True)
print (fnum)
# List of strings
str = ["Banana", "Cat", "Apple", "Dog", "Fish"]
# sorting and printing
str.sort(reverse=True)
print (str)
The output of the above program will be:
[50, 40, 30, 20, 10]
[20.45, 11.0, 10.23, 10.12, 0.1]
['Fish', 'Dog', 'Cat', 'Banana', 'Apple']
Python List Programs »