Differences between sort() and sorted() in Python

Sorting is a very common problem that many programmers may come across. Like any other programming languages, Python has its own built-in sorting methods.

These sorting methods are sort() and sorted().

In this post, I will share the differences between these two methods and how these can be used in programs. I found these methods important while solving some Kattis problems.

Common sorting problems are to sort some array of data in some order – either in ascending or in descending order.

sort()

The sort() sorts the elements in a list in a specific order, in ascending or in descending order.

Syntax:

list.sort(key=..., reverse=...)Code language: PHP (php)

sorted()

The sorted() method is used as an alternative to the sort() method as:

sorted(list, key=..., reverse=...)Code language: PHP (php)

In the above methods, key defines the condition on which the list is to be sorted. The reverse takes a Boolean value.

So what is the difference?

To understand the difference between them, first, you must know how they are being used.

How to use the sort() method

Say you have a list of unordered vowel and you want to sort it alphabetically.

# unordered vowels list
vowels = ['e', 'a', 'u', 'o', 'i']

# sort the vowels
vowels.sort()

# print vowels
print('Sorted list of vowels:', vowels)Code language: PHP (php)

The above program gives the following output.

Sorted list of vowels: ['a', 'e', 'i', 'o', 'u']Code language: CSS (css)

How to use the sorted() method

The above code can be alternatively written as:

# unordered vowels list
vowels = ['e', 'a', 'u', 'o', 'i']
# print vowels
print ('Sorted list of vowels:', sorted(vowels))Code language: PHP (php)

This gives the same output as the previous code.

Sorted list of vowels: ['a', 'e', 'i', 'o', 'u']Code language: CSS (css)

From the above examples, you can conclude a very important difference between these methods.

The Difference

a. sort() method doesn’t return any value. Rather, it changes the original list.

# unordered vowels list
vowels = ['e', 'a', 'u', 'o', 'i']

# sort the vowels
vowels.sort()

# print vowels
print('Sorted list of vowels:', vowels)
Code language: PHP (php)

In the above example, the order of the elements in the list vowels is changed.

b. sorted() returns a list with the elements in a sorted manner, without modifying the original sequence. 

# unordered vowels list
vowels = ['e', 'a', 'u', 'o', 'i']
# print vowels
print ('Sorted list of vowels:', sorted(vowels))

# print original list
print('Sorted list of vowels:', vowels)Code language: PHP (php)

The output:

Sorted list of vowels: ['a', 'e', 'i', 'o', 'u']
Sorted list of vowels: ['e', 'a', 'u', 'o', 'i']Code language: CSS (css)

In my previous post, I have shared about shared how to use switch-case conditions in Python as there is no switch-case construct in Python.

Leave a Reply

Discover more from BHUTAN IO

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top