Javatpoint Logo
Javatpoint Logo

Python sorted() Function

Python sorted() function is used to sort a sequence of elements. The sorted() function can be used to sort a variety of data structures or complex objects effortlessly in ascending or descending order. By default, it sorts elements in ascending order.

The sorted function takes three arguments and returns a list in sorted order. In the case of a dictionary, it returns a list of sorted keys. The time complexity of the sorted function is O(n logn).

The sorted function does not sort the passed sequence but creates a new sorted list. To sort a sequence, use the sort function rather than the sorted function.

In this article, we will explore the complexities, applications, advantages, and utility of sorted function in different scenarios.

The signature or syntax of the sorted function is given below:

Signature or Syntax

Parameters

  • iterable: It is the required parameter that represents an iterable.
  • key: It is an optional argument that represents a function. The default value is None.
  • reverse: It is also an optional Boolean parameter, and the default value is 'False'. One can pass the reverse parameter 'True' to get the sorted list in reverse order.

Return

It returns a new list containing the elements in a sorted order.

Let's see some examples of sorted() function to understand its functionality.

Python sorted() Function Example 1 - Sorting string elements

Here, in this example, we are sorting a string object to understand the idea behind the sorted function.

Output:

Sorted String Elements: ['a', 'a', 'i', 'j', 'n', 'o', 'p', 't', 't', 'v']

Explanation:

In the above example, the sorted function sorts each alphabet in the string "javatpoint" alphabetically and returns a list containing elements in a sorted manner.

Python sorted() Function Example 2 - Sorting list, tuple, and dictionary elements using sorted function

We can use the sorted function to sort any iterable, like a list, tuple, or dictionary. See the example below.

Output:

Sorted List elements: [56, 98, 622, 659, 1002, 2003, 3652]
Sorted Tuple Elements: [21, 232, 2500, 2578, 3698, 5264]
Sorted Dictionary Keys: [1, 2, 3, 4]

Explanation:

In the above example, the sorted function sorts the elements of the list and tuple and returns separate sorted lists. In the case of the dictionary, it sorts the keys only and returns a list containing keys in a sorted manner.

Python sorted() Function Example 3 - Sorting list elements in reverse order using the sorted function

To sort the list into reverse order (descending), pass True in the reverse parameter, and we will get the list sorted in reverse order.

Output:

Sorted list elements: [3652, 2003, 1002, 659, 622, 98, 56]

Explanation:

In the above example, we have passed Ture in the reverse argument that sorts the list elements in reverse or descending order and returns a list containing elements in descending order.

Python sorted() Function Example 4 - Exploring Key Argument with Lambda Function

The key parameter is used to customize the sorting criteria. The key parameter takes a function that produces a key known as a comparison key for each element. Here, what happens is that the key function takes each element from the iterable and generates a corresponding comparison key for that element based on the function passed. The sorted function uses those comparison keys during the sorting process.

The key argument is set to None by default. For example, when you required to sort a list of strings based on the length of the string. You can pass the 'len' function as the key parameter.

Here, we are sorting a list of tuples by passing a lambda function in the key during the call, which returns the sum of the elements for each tuple.

Output:

Sorted list of tuples: [(3, 5), (8, 5), (2, 15), (65, 5)]

Explanation:

In the above example, the sorted function sorts the elements (tuples) based on the sum of its elements. The lambda function takes each element (tuple) from the key and returns the corresponding sum values as the comparison keys. The sorted function uses those keys to sort the elements of the list.

Python sorted() Function Example 5 - Exploring Key Argument with Simple Function

In this example, we are passing a sum function instead of a lambda function as the key argument in the same example.

Output:

Sorted list of tuples: [(3, 5), (8, 5), (2, 15), (65, 5)]

Explanation:

In Python, the sum is an inbuilt function that returns the sum of values. In the above example, the key parameter takes each tuple from the list and passes it to the sum function. The sum function returns the corresponding sum value of each tuple. The sorted function then uses these sum values as a comparison key during the sorting process.

Advantages of the sorted function:

Here are some potential advantages of the sorted function below:

  1. Easy to use - The sorted function is very easy to use. It requires only one mandatory parameter, which is the iterable, and other parameters key and reverse, can be used to customize the sorting.
  2. Immutability - The sorting function creates a new sorted list leaving the passed sequence unchanged. This feature can be useful when we want sorted data as well as no effects on the original data.
  3. Customizable Sorting - The key parameter can be used to set the sorting criteria. By using this feature, we can easily customize the sorting process. Also, we can use the reverse parameter to sort the data reverse order.

Applications of the sorted function:

Some of the common applications of sorted functions are listed below:

  1. Data Analysis - The sorted function is frequently utilized where sorted data is required. By using the sorted function, data analysts can easily generate insights.
  2. Custom Sorting - When we want to sort complex objects, we can use the key features to set the sorting criteria of our specific demand.
  3. Displaying Results - When we want to display the result in a sorted manner to our users, we can use the sorted function to sort the data.

CONCLUSION:

With the help of Python's sorted() function, we may quickly and effectively sort a variety of data structures. It may be used for various sorting circumstances, from simple lists and strings to complex objects, thanks to its flexibility and simplicity. We may customize the sorting process to meet our particular demands by using the key parameter. The sorted() function in Python provides a dependable and simple method for organizing our data, making it an invaluable tool for any coders.


Next TopicPython Set





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