Difference between Sort and Sorted in Python List by Performance

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 the given list in 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 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 the above examples you can see this difference.)
  • Unlike sorted() function, the sort() method sort the elements of the original list.

I 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

#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().

Which one to use?

It purely depends on your requirements.

  • If you don’t require an original list in the laster part of your code, you can sort the original list. Use sort() list method.
  • If you require an 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 the Python list. If you have any queries, write in the comment section.

4 Comments

  1. Hi

    Please fix the last line
    from

    print("Time taken by sort(): %s seconds" % (time.time() - st))

    to

    print("Time taken by sort(): %s seconds" % (time.time() - start_time))

Leave a Reply

Your email address will not be published. Required fields are marked *