Python sorted() Function



The Python sorted() function is a built-in function that returns a new sorted list from the items in an iterable object. The order of sorting can be set to either ascending or descending. However, strings are sorted alphabetically, and numbers are sorted numerically.

An iterable is an object that enables us to access one item at a time by iterating over them, such as list, tuples, strings, etc.

Syntax

The syntax of the Python sorted() function is shown below −

sorted(iterObject, key, reverse)

Parameters

The Python sorted() function accepts three parameters, all of which are optional −

  • iterObject − It represents an object such as a list, string, or tuple.

  • key − It specifies a function representing a comparison key.

  • reverse − This parameter specifies the sorting order. If its value is set to True, the order will be descending and ascending if set to False. The default value is False.

Return Value

The Python sorted() function returns a sorted list.

Examples

Now, let's understand the working of sorted() function with some examples −

Example 1

The list with strings will be sorted alphabetically. The following example shows the usage of Python sorted() function where we are creating a list and trying to sort it.

orgnlStrList = ["Simply", "Easy", "Learning", "Tutorials", "Point"]
print("Before Sorting:", orgnlStrList)
print("Printing the list items after sorting:")
print(sorted(orgnlStrList))

When we run above program, it produces following result −

Before Sorting: ['Simply', 'Easy', 'Learning', 'Tutorials', 'Point']
Printing the list items after sorting:
['Easy', 'Learning', 'Point', 'Simply', 'Tutorials']

Example 2

By default, the lists with numbers are sorted numerically. In the code below, we have defined a list containing numbers and sorted that list in numerical order.

numericList = [88, 89, 81, 82, 86, 85, 83]
print("Before Sorting:", numericList)
print("Printing the list items after sorting:")
print(sorted(numericList))

Following is an output of the above code −

Before Sorting: [88, 89, 81, 82, 86, 85, 83]
Printing the list items after sorting:
[81, 82, 83, 85, 86, 88, 89]

Example 3

When we specify the value of "key" argument to "len", then, the lengths of the strings will be compared while sorting the list, rather than the strings themselves. The code below demonstrates how to sort a list based on the length of items.

orgnlStrList = ["Simply", "Easy", "Learning", "Tutorials", "Point"]
print("Before Sorting:", orgnlStrList)
print("sorting the list items by length:")
print(sorted(orgnlStrList, key=len))

Output of the above code is as follows −

Before Sorting: ['Simply', 'Easy', 'Learning', 'Tutorials', 'Point']
sorting the list items by length:
['Easy', 'Point', 'Simply', 'Learning', 'Tutorials']

Example 4

As previously stated, if the value of the "reverse" argument is set to "True", then the sorting order will be descending. In the code below a numerical list is created. Then we print its items in descending order.

numericList = [12, 24, 36, 48, 60, 72, 84]
print("Before Sorting:", numericList)
print("sorting the list items in descending order:")
print(sorted(numericList, reverse=True))

Following is an output of the above code −

Before Sorting: [12, 24, 36, 48, 60, 72, 84]
sorting the list items in descending order:
[84, 72, 60, 48, 36, 24, 12]
python_built_in_functions.htm
Advertisements