Difference between sorted() and list.sort() method

Posted on Updated on

There are different ways to sort a list in Python. You can find examples to sort a list in our article sort list in Python. Most popular ways are using sorted() and list.sort() methods. Each method has some advantage over other. In this article, we will be discussing difference between sorted() and list.sort() method.

Common difference between sorted() and list.sort()

sorted() list.sort()
sorted returns a <b>new</b> sorted list, leaving the original list unaffected. list.sort() sorts the list <b>in-place</b>, mutating the list indices, and returns none.
Use sorted() when you want to sort something that is an iterable, not a list yet. Use list.sort() when you want to mutate the list.
sorted() is slower as compare to list.sort() for list. list.sort() is faster as compare to sorted().
sorted() is applicable to sort any iterable object including list. list.sort() is applicable only to sort a list.
As sorted() returns new list, it is possible to retrieve original positions of the elements. After calling list.sort(), oe can't retrieve the original positions of the elements.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.