Sort() and sorted() are two different functions. In fact, the sort() is a list method whereas sorted() is a built-in function.
Even both sort() and sorted() sorts the elements in the list, they are different.
Table of Contents
Let’s take an example of the list where all the elements are of integer data types. We are sorting the given list in both ways.
>> listObj=[34,17,56] >> sorted(listObj) [17, 34, 56] >> listObj [34, 17, 56]
By default, it will sort the elements in ascending order.
How to sort the list in descending order using sorted() function?
You have to pass “True” to the second parameter “reverse” of the sorted() function.
>> listObj=[34,17,56] >> sorted(listObj, reverse=True) [56, 34, 17]
>> listObj=[34,17,56] >> listObj.sort() >> listObj [17, 34, 56]
Method sort() also sort the list in ascending order by default.
How to sort the list in descending order using sort() method?
As we have seen in sorted() function, pass “True” to the second parameter “reverse” of the sorted() function.
>> listObj=[34,17,56] >> sort(listObj, reverse=True) >> listObj [56, 34, 17]
Listing one by one.
I hope the given examples will give a clear idea.
Which one is better by performance?
Let’s find it out.
Creating a list of 1000000 integers selected randomly.
import time
import random
listObj=[]
for i in range(1000000):
listObj.append(random.randint(1,1000))
listObj2 = listObj
#sorting list using sorted() built-in function
st = time.time()
sorted(listObj)
print("Time taken by sorted(): %s seconds" % (time.time() - st))
#sorting list using using list sort() method
st = time.time()
listObj2.sort()
print("Time taken by sort(): %s seconds" % (time.time() - st))
Output:
Case 1:
Time taken by sorted(): 0.4520258903503418 seconds Time taken by sort(): 0.28101587295532227 seconds
Here, sort() method is executing faster than sorted() function.
Case 2:
Time taken by sorted(): 0.2960169315338135 seconds Time taken by sort(): 0.3980228900909424 seconds
Here, sorted() function method is executing faster than sort() function.
There is also a difference in execution time in both cases. It is clear, the time taken to sort the elements depends on the elements in the list rather than which method or function you are using.
Note:
In the above example, we have used loop for creating a list of 1000000 elements. You can also use the list comprehension technique to create the list.
import random
listObj = [random.randint(1, 1000) for i in range(1000000)]
This is the convenient way of creating list.
Coming back to the main point of comparing sort() and sorted().
It purely depends on your requirements.
If you are new to the Python programming, check out a complete cheat sheet of Python 3 syntax.
This is all about the main difference between sort and sorted in the Python list. If you have any queries, write in the comment section.
Hi
Please fix the last line
from
to
Thanks for the correction, Aman! We fixed it.
Hope you enjoy reading other tutorials.
Take care!
very useful, thank you <3
You’re welcome! Hope you enjoy going through other tutorials as well.