The sort method for Python lists is a built-in function that enables users to organize the elements of the list in either ascending or descending sequence. This method does not produce a return value since it does not generate a new list; rather, it alters the existing list in place. The sort function serves as a valuable tool for developers, facilitating the arrangement of list items in either numerical or alphabetical order.
Syntax of the sort Method in Python List
The syntax for the list sort method is as follows:
Syntax:
list_name.sort(key = None, reverse = False)
Parameter:
- key (Optional): This parameter enables the user to define a function that will serve as the basis for sorting criteria. For instance, if you wish to arrange the list according to its length, you can effortlessly utilize the len function.
- reverse (Optional): This parameter accepts a Boolean value. When you assign the reverse argument a Boolean value of True, the list will be organized in descending order; otherwise, it will be sorted in ascending order. By default, this parameter is set to False.
Return:
The sort method in Python returns none.
Examples of List sort
We will now examine a few instances of the list sort function in Python.
Example 1: Sort List in Ascending Order
In the subsequent illustration, we will explore the functionality of the sort method, which is utilized to arrange a specified list in ascending order.
Example
lst_1 = [1,5,7,0,10,14]
# Sort the value in ascending order
lst_1.sort()
print(lst_1)
Output:
[0, 1, 5, 7, 10, 14]
Explanation:
In the example provided, we have arranged the specified list in ascending order by utilizing the sort method.
Example 2: Sort List in Descending Order
To arrange the provided list in descending order, we can utilize the sort method by assigning the reverse parameter a value of True. This can be illustrated in the example below:
Example
# given list
lst_1 = [1,5,7,0,10,14]
# Sort the value in descending order
lst_1.sort(reverse = True)
print(lst_1)
Output:
[14, 10, 7, 5, 1, 0]
Explanation:
In this instance, we have inverted the sequence of the sorted list by specifying the reverse parameter as True.
Example 3: Sort a List in order of the Length of the Values
We are going to define a function that will arrange the provided list based on the length of its elements, as demonstrated in the example below:
Example
# defining a function that returns the length of each element
def newFunc(e):
return len(e)
# creating the list
fruits = ['Apple', 'Strawberry', 'Melon', 'Orange']
# sorting the fruits names in ascending of the length of the fruits name
fruits.sort(key = newFunc)
# printing the fruits name
print(fruits)
Output:
['Apple', 'Melon', 'Orange', 'Strawberry']
Explanation:
In this instance, we have utilized the key parameter in the sort method and assigned it to the function we created. This function calculates the length of each item in the list and organizes them in ascending order based on their lengths.
Things to Remember
- The Python sort method is used to modify the list in place.
- The Python sort method returns None .
- By default, the sort function sorts the list in ascending order . You can also sort the list in descending order, all you need to do is to set the reverse parament to true.
- The key parameter can be used for customizing the sorting criteria.
- We can use the sort method, if we need to create a new sorted list without changing the original list.
- By default, the sort method is case sensitive . It means all the capital letters will be sorted before.
Conclusion
Python offers a variety of built-in functions and methods that assist developers in efficiently crafting error-free applications. Among these, the list sort method is particularly advantageous when you need to arrange a list quickly, whether it be in alphabetical order, numerical order, or in either ascending or descending fashion. This tutorial has provided comprehensive insights and examples related to this method, but do not confine yourself to just these instances; instead, experiment with additional examples to enhance your proficiency.
The sort method yields a sorted list of the elements.
The sort function yields no return value because it alters the original list directly rather than generating a new one.
- Is it possible to utilize the sort method on a list that has a combination of different data types?
The sort function is incapable of arranging a list that includes various data types. If a user attempts to perform such an operation, Python will generate a TypeError. This occurs because the differing types prevent the comparison of the various elements within the list.
- What distinguishes the sort method from the sorted function in Python?
| Characteristics | Sort() | Sorted() |
|---|---|---|
| Definition | The sort() method in Python returns none as it sorts the element of the original list in its place. | The sorted() method in Python returns a new list after sorting the list. |
| Parameters | It accepts two arguments i.e., key and reverse. | It accepts three parameters i.e., iterable, key, and reverse. |