Javatpoint Logo
Javatpoint Logo

Python List sort() Method

The Python sort() method sorts the list elements effortlessly. The sort function is a part of the list object that allows programmers to sort the elements of a list in descending or ascending order.

The sort() function takes an optional parameter 'reverse', which sorts the list into descending order when it is True. By default, the sort function sorts the list elements into ascending order.

The sort() function also takes an optional parameter 'key' that allows us to pass a function (a callable) that needs to be called for each element in the list in order to produce the corresponding comparison keys.

In this article, we'll explore the various aspects of the Python list.sort() function and understand in detail. The examples and signatures of the sort() function are given below.

Signature or Syntax of Sort() function:

Parameters

  • iterable = It is a required parameter. The list is the iterable we want to sort is passed to the sort function.
  • key = It is an optional parameter. It requires a function that produces a comparison key for each element in the list. The default value is None.
  • reverse - It is also an optional Boolean parameter. The default value is False. The sort function sorts the list element in descending order when the reverse parameter is True.

Return

It sorts the list and returns None.

Let's see some examples of the sort() method to understand its functionality.

Python List sort() Method Example 1 - Sorting lists of characters and integers in ascending order

It is a simple example that sorts two lists in ascending order. See the example below.

Output:

Original apple list: ['a', 'p', 'p', 'l', 'e']
Original even list: [6, 8, 2, 4]

After Sorting:
Apple list: ['a', 'e', 'l', 'p', 'p']
Even list [2, 4, 6, 8]

Explanation:

In the above example, we have created two lists, apple (a list of characters) and even (a list of even integers). Before sorting these lists, we printed the original list into the console. Following that, we have used the list.sort() function to sort both lists. Finally, we printed the sorted lists into the console. The sort function has sorted both the list cleverly and effortlessly.

Python List sort() Method Example 2 - Sorting lists of characters and integers in descending order

This example sorts the list into descending order.

Output:

Sorted apple list: ['p', 'p', 'l', 'e', 'a']
sorted even list: [8, 6, 4, 2]

Explanation:

In the above example, we first created two lists, apple (a list of characters) and even ( a list of even integers). To sort these lists in descending order, we set reverse=True. The sort method then sorts these lists in descending order. Finally, we printed the sorted lists into the console.

Python List sort() Method Example 3 - Sorting lists with a Custom Key

The given example illustrates how to pass a function as a 'key' parameter:

Output:

Original List: ['Java', 'Python', 'C', 'C++', 'Ruby']
Sorted List: ['C', 'C++', 'Java', 'Ruby', 'Python']

Explanation:

In the above example, we first created a list of strings and printed it into the console. Then we used the sort function to sort this list and passed the 'len' function as a key. The 'len' function returns the respective lengths of each element as a comparison key.

The sort function uses these comparison keys to sort the list based on the lengths of the strings. In the case of a tie, the elements are sorted in the lexicographical order. Finally, we printed the sorted list into the console.

Sort vs. Sorted function

The following are the key differences between the sort and sorted functions:

Sort Sorted
The sort function sorts the original list. The sorted function does not sort the original list instead, it returns a new sorted list.
The sort function does not return anything (None). So, it cannot be assigned to a variable. The sorted function returns a sorted list and can be assigned to a variable.
Syntax - list.sort(key=None, reverse=False) Syntax - sorted( iterable, key=None, reverse=False)
The sort function cannot be used when preserving the original list is required. The sorted function is used when preserving the original list is required.
The sort function performs better than the sorted function as it reduces the overload of creating a new list. The sorted function does not perform better than the sort function as it has an overload of creating a new sorted list.

In summary, while preserving the original list is necessary, one should use the sorted function. While preserving the original list is not necessary, one can use the sort function.

Conclusion:

The sort() function in Python is a simple tool that simplifies the sorting process of lists. Whether sorting a list in ascending or descending order or applying a custom sorting criterion, the sort() function provides flexibility and ease of use. By understanding its parameters and capabilities, we can efficiently organize our data with just a single function call. So the next time you need to sort a list in Python, remember the sort() function and let it do the heavy tasks for you.


Next TopicPython Lists





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA