How to Alphabetically or Numerically Sort a List in Python



Python


In this article, we show how to alphabetically or numerically sort a list in Python.

We will show how to sort a list in ascending order and in descending order.

This task is actually pretty simple, because Python has a built-in sort() function, which allows us to sort lists very easily.

To sort lists in ascending order, we take the list name and use the sort() function on the list.

To sort lists in descending order, we take the list name and use the sort(reverse=True) function on the list.

Below are examples of lists sorted in ascending and descending order.



So let's now go over this code.

In the first list, we have alphabetical characters.

We're able to list these alphabetical characters in ascending order with the sort() function.

By specifying the parameter, reverse=True, in the sort() function, we now have the alphabetical characters in descending order.

In the second list, we have numbers.

We're able to list these numbers in ascending order with the sort() function.

By specifying the parameter, reverse=True, in the sort() function, we now have the numbers in descending order.

And this is how we can alphabetically or numerically sort a list in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...