Difference between sort() and sorted() functions in Python

Hey everyone, in this tutorial we are going to learn how to use sort() and sorted() functions in our Python program. We are going to see how these two functions are quite different even though they serve the same purpose. Let’s discuss these two in detail.

sort() vs sorted() functions in Python

As it is clear from the name that both of these functions are used for sorting. But they have different syntax and return type. Let’s discuss them one by one.

sort()

This function belongs to the list class in Python and can only be used with lists. The syntax for the sort() function is as follows:

list_name.sort(key, reverse = False)

In the above syntax, the parameter key is a function based on which we want to sort our list. The second parameter reverse is used to specify whether we want to sort the list in ascending order or the descending order. If this is set as True then the list is sorted in descending order, if False, then in ascending order. The default value for this parameter is False. Therefore, if we do not pass any value for this parameter, the list is sorted in ascending order. Both the parameters in the function sort() are optional. Another point to note is that this function modifies the list and once sorted, we cannot access the previous unsorted list.

An example program to explain the working of sort() function has been given here.

li = [1,6,3,89,5]
li.sort()

print(li)

Output:

[1, 3, 5, 6, 89]

We can use ”reverse = True” to sort() the list in descending order. See below.

li = [1,6,3,89,5]
li.sort(reverse = True)

print(li)

Output:

[89, 6, 5, 3, 1]

sorted()

The function sorted() can be used to sort any iterable. i. e. list, dictionary, tuple, etc. The syntax for the sorted() function is as follows:

sorted(iterable_name, key, reverse = False)

In the above syntax, iterable_name refers to the iterable that we want to sort. The second and third parameters, key and reverse, are the same as in sort() function. The default value for reverse is False. An important point to note is that this function does not alter the iterable that is passed. Instead, it returns a new iterable which is sorted form of the iterable passed in the function.

See the below example program to understand the concept in a better way.

li = [1, 6, 3, 89, 5]
li_new = sorted(li)

print("Given list is {}.".format(li))
print("Sorted list is {}.".format(li_new))

Output:

Given list is [1, 6, 3, 89, 5].
Sorted list is [1, 3, 5, 6, 89].

To sort in descending order…

li = [1, 6, 3, 89, 5]
li_new = sorted(li, reverse = True)

print("Given list is {}.".format(li))
print("Sorted list is {}.".format(li_new))

Output:

Given list is [1, 6, 3, 89, 5].
Sorted list is [89, 6, 5, 3, 1].

Using key parameter in sort() and sorted()

For the key parameter in the functions, sort() and sorted(), we pass a function which is the basis of comparison to sort the elements in the iterable. Let’s say, we want to sort a given tuple of lists according to the sum of the elements in each list. To do this, we create a function that returns the sum of the list and then we pass this function as key in our sorting function.

See the example code.

def ListSum(element):
    return sum(element)

t = ([1, 2], [2, 6], [4], [6, 7])
t_new = sorted(t, key= ListSum)

print("Given tuple is {}.".format(t))
print("Sorted tuple is {}.".format(t_new))

Output:

Given tuple is ([1, 2], [2, 6], [4], [6, 7]).
Sorted tuple is [[1, 2], [4], [2, 6], [6, 7]].

We can use the key parameter with sort() function in the same way.

Thank you.

Also, read: How to implement Topological Sorting in Python

Leave a Reply