Difference between Sort and Sorted in Python List by Performance
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.
How to sort a Python List?
Let’s take an example of the list where all the elements are of integer data types. We are sorting given list with both ways.
Example: Using Sorted() Built-In Function
>> 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]
Example: Using sort() Python List Method:
>> 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 like 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]
What is the difference between sort and sorted in Python List?
Listing one by one.
- The sorted() is a built-in function whereas sort() is a Python list method.
- The sorted() takes the list as an argument. The sort() is the method of a list object.
- The sorted() returns the new sorted list whereas sort() method does not return anything. (In above examples you can see this difference.)
- Unlike sorted() function, the sort() method sort the elements of the original list.
Hope the given examples will give a clear idea.
Sort vs Sorted Performance
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 st = time.time() sorted(listObj) print("Time taken by sorted(): %s seconds" % (time.time() - st)) start_time = time.time() listObj2.sort() print("Time taken by sort(): %s seconds" % (time.time() - st))
Time taken by sorted(): 0.4520258903503418 seconds Time taken by sort(): 0.28101587295532227 seconds
Here, sort() method is executing faster than sorted() function.
Time taken by sorted(): 0.2960169315338135 seconds Time taken by sort(): 0.3980228900909424 seconds
Here, sorted() function method is executing faster than sort() function.
And 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.
Which one to use?
It purely depends on your requirement.
- If you don’t require an original list in laster part of your code, you can sort the original list. Use sort() list method.
- If you require optional list later part of your code, you need to create a new sorted list. Use sorted() built-in function.
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 Python list. If you have any query, write in the comment section.