Last Updated on May 30, 2024 by Roshan Parihar
To sort a list in ascending or descending order in Python, use the sort()
function with reverse
as an argument with its value as false or true.
Sort a List in Ascending Order in Python
You can sort the list items in ascending order using the Python sort()
function. Whether it is an integer or string, you can perform sorting using the examples given below.
Integer List Items
If there are integer elements in the list, you can sort them in ascending using the below method. Use the sort()
function and pass the argument as reverse=False
.
Example 1
1 2 3 |
myList = [21, 10, 18, 9]; myList.sort(reverse=False); print(myList); |
Output
The above example contains the list of items arranged in ascending order using Python. The elements of the list contain the integer items without any sequence. You can arrange the list item to ascend easily using the sort function as given below.
String List Items
You can also sort the list with string elements in ascending order using the same sort()
function. Pass the reverse=False
as the argument of the function as given below.
Example 2
1 2 3 |
myList = ['ten', 'one', 'eleven', 'six']; myList.sort(reverse=False); print(myList); |
Output
The above example contains the alphabetically arranged list items in ascending order. You can create your ascending elements and arrange them sequentially using the above method.
Sorting A List in Descending Order in Python
You can also sort the list in descending order using Python. Use the same sort()
function with the list variable. Now, you have to pass reverse=True
as the argument of the sort function.
The resulting output gives the sorted list in a descending manner. You can also arrange the string as well as the integer list items in ascending or descending.
Integer List Items
If there are only integer items on the list, you can arrange them in descending using sort()
.
The below example contains the list of elements not arranged in a mannered way. However, to manage the list items in descending order, you have to use the below-given method.
Example 3
1 2 3 |
myList = [34, 11, 21, 7]; myList.sort(reverse=True); print(myList); |
Output
The above example shows the list of integer items in the output. The output contains the sorted list elements in descending order. There are 4 elements in the above list and arranged sequentially.
String List Items
Again, if you want to sort the list containing the string elements using Python. You have to use the below-given example using the sort()
.
Example 4
1 2 3 |
myList = ['nine', 'two', 'twelve', 'seven']; myList.sort(reverse=True); print(myList); |
Output
The above example shows the string list items arranged in descending order. The elements are sorted according to the alphabets at the start of each string element.
Sorting of Python List Contains Both Integers and Strings
The above examples showing examples contain only integers or strings. However, you cannot perform sorting over the list containing both string and integer elements together.
It may be possible that you would like to sort a list containing integers and strings in combination. But, you don’t have an idea about the list items and you perform sorting over it mistakenly.
The below example is the list containing both the string and integer elements. If you perform sorting with the below list, it has given an error message in the output.
Example 5
1 2 3 |
myList = ['nine', 'two', 'twelve', 'seven', 34, 11, 21, 7]; myList.sort(reverse=True); print(myList); |
Output
File “demo.py”, line 2, in <module>
myList.sort(reverse=True);
TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’
The above example shows the error message after you perform sorting. The above list contains both the string and the integers. it’s not possible to arrange them sequentially in ascending or descending order.
DOWNLOAD Free PYTHON CHEAT SHEET
You may also like to read