Python List sort() MethodThe 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
ReturnIt 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 orderIt 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 orderThis 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 KeyThe 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 functionThe following are the key differences between the sort and sorted functions:
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
|