Sort a List in Ascending or Descending Order in Python

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

Output

[9, 10, 18, 21]

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

Output

[‘eleven’, ‘one’, ‘six’, ‘ten’]

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.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

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

Output

[34, 21, 11, 7]

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

Output

[‘two’, ‘twelve’, ‘seven’, ‘nine’]

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

Output

Traceback (most recent call last):
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