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 List sort() Method - WTMatter

Python List sort() Method

Python List Sort() Method

This is a tutorial of the Python List sort() method. Learn to sort a Python List into Ascending or Descending order & using a particular key with examples.

Python List sort() Method

The sort() method allows you to sort a given Python List in a given order. The order can be Ascending or Descending. This method also allows you to arrange given list items according to a particular key.

Syntax

You can directly apply the sort() method on any Python List object as illustrated by the following syntax.

sort() Parameters

The Python List sort() method can take two different arguments but both of these are optional to be provided. These are briefly defined below.

  • key. The key Parameter is used to specify the key function according to which you want to sort the current list. We’ll find out more about this parameter in the examples section.
  • reverse. This is a boolean type parameter which is set to False by default. It is used to specify the order in which you want to sort the list. If set to False, the given Python List will be sorted in ascending order and if set to False, then the list will be sorted into Descending order.

sort() Return Value

sort() method itself does not return any value. i.e. It returns None.

Note. In case, you do just want to get the sorted list as a return value and do not want to shift the position of the elements in the original list, you can make use of the sorted() function instead of the sort() method.

Examples

I’ve provided below a few illustrative examples to demonstrate the use of the Python List sort() method for different types of sorting operations.

Example 1. Sorting a List in Ascending Order

Most of the time we require our list just to be sorted in Ascending Order and that we can do like breeze with this method as depicted below.

Here we’re sorting two different lists in Ascending order. In the first list, we’ve some random integers that we’ve sorted into Ascending order using the sort() method and similarly in the second case, we’ve sorted a list containing Name strings.

Python List Sort() Method Example Ascending Order

Example 2. Reverse Sort (Sorting Python List in Descending Order)

To Reverse Sort a list i.e. sorting the list in the Descending order, we just have to specify the argument reverse of the method sort() as True as illustrated in the following example.

The following output screenshot clearly shows that the two lists are now reverse sorted i.e. in descending order.

Python List Sort() Method Example Descending Order Or Reverse Sorting

Sorting Lists using Key Parameter

As described earlier, the method sort() also has a key parameter. We can provide a function as an argument for the key parameter to sort a given Python List according to our own logic. The logic according to which you want to sort a list has to be defined within a function and that function has to be provided as the argument for the key parameter.

Instead of defining your own logic functions, this function can also accept many built-in python functions as the key argument. Let’s say you want to sort a given list according to the length of the list items. The items with the shortest length should come first and the items with the largest string length should come at the end of the list. To achieve this kind of sorting, you just have to specify the argument key = len as len() is the Python’s built-in function to calculate the length of any Python object.

The proper syntax is defined below.

The same sorting process can also be done using Python’s built-in function sorted() as defined below.

The key parameter, therefore, opens a whole new set of sorting opportunities with the Python List.

Example 3. Sorting List using Key Argument & Built-in len() Function

In this example, we have a list of some strings and each of these strings has a different length. So, here we’re sorting the list according to the length of the strings in ascending as well as in the descending order.

Python Sort() Method Example With Key Parameter

Example 4. Sorting is a Python List using Custom Logic

Here also we’ll make use fo the key argument but to this argument, we’ll provide our own defined custom function containing the logic of sorting.

I’ve taken a Python List that contains tuples as the items. Each tuple contains 3 numbers. The list is now sorted according to the ascending order of the sum of the numbers in each tuple item.

Python Sort() Method Example With Key Argument And Custom Function

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not 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 *