How To Sorting List in Ascending or Descending in Python

In this tutorial, learn sorting list in ascending or descending using Python. The short answer is to use the sort() to sort Python list items that can be strings 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 Python sort() for sorting the 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 the sort function, you can print the resulted list to get the sorted list.

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 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 the 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.

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

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 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.

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 element.

Sorting of Python List Contain Both Integers and Strings

The above examples showing examples contain 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 a 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 the 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.

DOWNLOAD Free PYTHON CHEAT SHEET

You may also like to read

I hope you like this post on how to sort the list in ascending or descending in Python.

References
Python Doc For List Sort