Sort function in Python helps sort a list into ascending or descending order. It can be used for sorting a numeric list, tuples, and string list. It does not create a separate list but modifies or sorts the original.

Syntax of Sort method in Python

The sort function has the following syntax as listed below: – The sort function has two optional parameters, namely as listed below: –

Key: – This is used to sort a function based on a sorting criterion. Reverse: – it takes the value as either true or false. If true, this custom function sorts the list in descending order.

Sorting a list in ascending order using the Sort method in Python

In Python, the sort function, by default, sorts any list in ascending order. A list in ascending order has the lowest value on the left-hand side, and the highest value comes on the right-hand side. Example: Python code: Output: Code Explanation:

The above list is a randomly defined string list. The sort function of Python helps in sorting the random list in ascending order, with the lowest length of the word on the left-hand side and the highest length of the word on the right-hand side.

Sorting a list in descending order

The sort function also allows a list to be sorted in descending order. It can be defined as the order where the list starts with the highest value and ends with the lowest value. The reverse parameter of the sort function is assigned as true to get a list sorted in descending order. Let us look at the below example: – Python code: Output: Note: Ensure that the assignment of the word “True” to the reverse parameter starts with uppercase “T” to avoid any run time errors.

Sorting a list of tuples using the Sort method in Python

Python Tuples are collections of immutable elements that follow an ordered sequence. Python’s sort function can be used to sort a list of tuples using a customized function and lambda expression. A customized function can be created without a name and represented by a lambda expression. Following is the syntax for Lambda expression: – Syntax: The above syntax is equivalent to the below python code: Here is an example of a tuple and customized function to illustrate how a sort function with key parameters helps sort the elements in a tuple:- Python code: Output: Code Explanation:

The reverse parameter is defined as true to sort the tuple in descending order. The customized function takes the second element of the tuple. This is utilized as the key of the sort function.

Let us look at the below example that makes use of lambda expression: Python code: Output: Explanation:

The lambda expression helps you to sort the elements of the tuple from high to low with the key as the second element of the tuple. The program will check the highest value of the second element of the tuple.

Sorting list items using Len as key parameter

Len is an in-built function thatdetermines the length of the item. The length determined can be used for the indexing in the sort function. To do this, we assign Len to the Python sort function’s key parameter. The following Python code illustrates how to use the Len function with the sort function. Python Code: Output: Code Explanation:

The length of the element becomes an index for the key parameter to arrange the list in ascending order. The Len function estimates the length of each element present in the base list. It keeps the shortest element on the left-hand side and the largest element on the right-hand side. Here, the words alto and java have lengths of 4, which are arranged first in the list, followed by Guru99, which has a length of 6.

This example illustrates the concept of the reverse parameter in the sort function as shown below: – Python Code: Output:

Sorting list items using user-defined function as a key parameter

You can also use a user-defined function as a key parameter to sort a list. Here is an example: – Python Code: Output: Code Explanation:

A user-defined function is created to return the year The key parameter takes a user-defined function as a criterion. Sort function will arrange the list in ascending order based on the value of the element “year”.

Difference between the sort method and sorted method in Python

Before the differences are summarized, let us understand the syntax of the Sorted method. Python Syntax: Code Explanation:

The sorted function sorts the list into either ascending order or descending order. The function accepts three parameters, out of which two parameters are of an optional type and one parameter is of the required type. Parameter list_name is required when using the Sorted method. The Sorted method can take any type of iterable list as input. Key parameters and reverse parameters are optional parameters under the sorted method in Python.

Here is an example of using the sort and sorted function in Python as shown below: – Python code: Output: Code Explanation:

The original list is passed as a required parameter for the sorted function. The Sorted method returns a new list besides the original list. Since no additional parameters are passed to the sorted method, the new list is by default sorted in ascending order. The sort function does not make any new list.

The following are the points on key similarities and differences: –

When to utilize the sorted method or the sort method?

Let us take an example that requires race data to be sorted. The program uses the bib number and time taken in seconds to finish the race. Python code: Output: Code Explanation:

The base list is sorted using both the sort function and sorted function. The base list is lost when the sort function is applied. The sort function should not be applied to the original data set. It should be used when there is a copy version of the original data set. The Sorted function retains the original list. It does not override it. This offers better traceability and effective data management.

Conclusion

The sort function helps in sorting a numeric and string list in python. It can also help in sorting tuples. Reverse and key are optional parameters of the function. If there is a need to have a separate list and simultaneously have the original list, then the sorted function must be used otherwise, sort function must be utilized to achieve optimized memory utilization.