Python sorted() Function

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

The sorted() function in Python is a library function that is used to sort an iterable object and returns an iterable containing all the elements of the passed iterable object in a sorted manner.

The iterable object is simply the object which can be iterated on, whether it be a list, dictionary, tuple or string, etc.

Syntax of sorted() function in Python

The syntax of sorted() in Python is as follows:

Parameters of sorted() function in Python

Below are the parameters of the sorted function:

  • iterable_object: It is the iterable object which needs to be sorted. It can be a sequence like string and list or any collection like set and dictionary or any other iterator object.
  • reverse (Optional): It specifies the sorting order, i.e., if the reverse is True, then descending, otherwise ascending.
  • key (Optional) It is the function that provides the key index by which the sort comparison takes place. For Example, if the array is [[2, 1], [1, 3]] and the key provided by this function is 0, then the sorted list will look like [[1, 3], [2, 1]] as sort comparison is according to the 0th index of all sublists.

Return Value of sorted() function in Python

Return Type: list

It returns a sorted list, according to the iterable passed, a list, tuple, dictionary, etc.

Example of sorted() function in Python

Output:

Explanation:

The myList is passed to the sorted function, which returns the sorted list, and the print function prints the return value, which is nothing but a sorted and duplicate version of the provided list.

What is sorted() function in Python?

Suppose you are instructed to sort a string according to the ASCII value of characters, and here you can't apply the sort() function because it will give an error with strings, so you will have to use a function that can work on every iterable object in python,

So here comes the sorted() function in python that will easily do the job of sorting the string according to ASCII value of the characters.

So why would you write the whole code for sorting when you can use the inbuilt function, which will store a sorted list in a variable. A sorted list can contain string values, numbers, etc., based on the type of values passed in the iterable object.

If the iterable contains numeric values, then sorting will be according to the value of the number (small to large), and if the values are of string type, then the sorted list will contain sorted strings according to their ASCII values.

As the name suggests, the sorted() function in python is used to return a newly created sorted list from an iterable object and store that newly created list in a variable.

More Examples

Example 1: Sorting a String and Tuple

Output:

Explanation:

The characters of the string are being sorted according to ASCII value and the tuple of the integer is being sorted according to the integral values of elements.

Example 2: Using the reverse parameter

Output:

Explanation:

In all 3 cases: list, string, and tuple values are sorted and then those sorted values are reversed, and then reversed sorted list is returned.

Example 2: Using the key parameter

In this example we are going to utilize the key of the sorted function.

Output:

Explanation:

  • The sorted() function is getting called with the key function which returns the key value by which the list is to be sorted.
  • In 1st case, list is sorted according to 1st index of the sublists.
  • In 2st case, the tuple is sorted according to 0th index of the subtuples.
  • In 3rd case, the list is sorted according to 0th index of the sublists.

Example 3: Sorting a dictionary using the sorted function.

Here we are going to sort a dictionary with the sorted function let's see the consequences.

Output:

Explanation:

This dictionary is sorted according to the key and a list which contains all the sorted keys of the dictionary is being returned by the sorted function.

Conclusion

  • The sorted() in python is used to store a sorted copy of an iterable object in the form of iterable.

  • It takes 3 parameters: iterable_list, reverse, and key.

  • Finally, the iterable returned can be iterated over, and all the sorted values can be easily accessed in that list.

  • This is a very useful function because it works well with immutable data types also. The reason is, that it creates a new iterable object for sorted data.

Read More: