Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python Sort List using sort() Method - WTMatter

Python Sort List using sort() Method

Python List Sorting

This is a detailed tutorial on sorting a python list in ascending, descending, alphabetical, reverse alphabetical or even in a custom specified order.

Sorting A Python List

Python Lists offers an inbuilt method sort() that you can always use to sort any list in either ascending or descending order. The syntax of this method written below. This method will allow you to sort the numerical lists in ascending or descending order and the string-based lists in alphabetical or reverse alphabetical order.

Syntax

The full syntax of the sort method is written below.

list.sort(reverse=True|False, key=aDefinedFunction)

Both of the parameters are actually optional. The reverse parameter has a default value as false while the key parameter in only needed when you’ve to sort according to your own user-defined order.

Let’s assume that arr is a list variable. Now follow the given syntax for sorting this list stored in the arr variable in ascending order.

arr.sort()

To sort the same list in descending order, all you need to provide the sort method with a reverse parameter with the value set as True.

arr.sort(reverse = True)

If you want to sort the list in a custom order, then you must specify a function that you’ve defined first in the key parameter of the sort() function. Now, this function is like the one defined below.

Now, you need to understand how you can make use of this function for the sorting purpose. Let’s say if I’m returning here len(par), then basically I’m sorting the list according to the length of the list elements in ascending order as the len() function evaluates the length of any Python Object.

This concept will be clearer to you when you’ll go through the examples.

Examples

Several examples have been given below for the clarity of the Python List Sorting concept using the sort() method.

Sorting Numerical List

In the following code, we’ve taken a list variable arr which contains different integers in random order. The to sort them in ascending as well as descending order, we’ve used the sort function.

The screenshot for the output is given below.

Python List Sorting Example

Sorting String-Based Lists

Similarly, you can also sort a list of strings in alphabetical order. An example illustrating the same is also given below.

Sorting List Alphabetical & Reverse Alphabetical Order Example

Sorting Python List in Custom Order

Let’s say we’ve sorted the list according to the length of the different elements of the list. So, here we need to make use of the key parameter of the sort() method. The illustrative code for the same is written below.

Python Custom List Order Example

Sorting Multi-Dimensional List

Now, let’s say if you have a multi-dimensional list and you want to sort the complete list according to the custom order of a particular sub-dimension, then also you can use the key parameter of the sort() method. An Example illustrating the same is given below.

Python Sort List Custom Order Multi Dimensional Example

You can clearly see first the list is sorted in ascending and descending order according to the first element of each sub-list item and then sorted in ascending and descending order according to the second element of each sub-list item.

Suggested Articles.

If you find this article useful, do share it with others who might be willing to learn Python. If you have any questions related to this article, feel free to ask in the comments section.

Don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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