In this tutorial, learn how to sorting list in ascending or descending using Python. You can sort Python list items which can be string or integers to ascending or descending order.

Sorting List in Ascending With Python

You can sort the list items to ascending using Python. To perform this task, you have to use the Python sort() for sorting list in ascending. To sort the list whether it is integer or string, you can perform sorting using the below example.

Example1: Integer List Items
If there are integer elements in the list, you can sort them to the ascending using the below method. Use the sort() and pass the argument as reverse=False. After executing the code with sort function, you can print the resulted list to get the sorted list.

Output

[9, 10, 18, 21]

The above example contains the list 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 ascending easily using the sort function as given below.

Sorting List in ascending in Python

Example2: String List Items
You can also sort the list contains string elements alphabetically in ascending order. Again use the same sort() to perform the sorting operation and pass reverse=False as the argument. It uses the first alphabetical letter of list element to arrange in ascending order.

Output

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

The above example contains the alphabetically arranged list items in ascending order. You can create your own ascending elements and arrange them sequentially using the above method.

How to Sort A List in Descending With 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 resulted 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.
Example3: Integer List Items
If there are only integers 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.

Output

[34, 21, 11, 7]

The above example showing the list of integers 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.
Example4: 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().

Output

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

The above example showing the string list items arranged in descending order. The elements are sorted according to the alphabets at the start of each string elements.

Sorting of Python List Contain Both Integers and Strings

The above all examples showing the examples contains only integers or strings. However, you cannot perform sorting over the list contain both string and integers elements together.

It may be possible that you may like to sort list contains 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 contains both the string and integer elements. If you perform sorting with below list, it has given an error message in the output.

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 showing the error message after you perform sorting. As the above list contains both the string and the integers. it’s not possible to arrange them sequentially in ascending or descending order.

You may also like to read

Hope, you like this post of how to sort the list in ascending or descending in Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to perform sorting over the list items.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.