Difference between sorted() and sort() function in Python

Let us see the difference between the sorted() and sort() function in Python:

Python sorted()

Python sort()

The sorted() function returns a sorted list of the specific iterable object. The sort() method sorts the list.
We can specify ascending or descending order while using the sorted() function It sorts the list in ascending order by default.
Syntax: sorted(iterable, key=key, reverse=reverse) Syntax: list.sort(reverse=True|False, key=myFunc)
Its return type is a sorted list. We can also use it for sorting a list in descending order.

Can be used with any iterable, even if comparison between elements is not defined

Requires elements to be comparable using the < and > operators

Maintains the relative order of equal elements, making it stable.

May not be stable; the order of equal elements may change.

O(n log n) time complexity for most cases.

O(n log n) time complexity for most cases.

It can only sort a list that contains only one type of value. It sorts the list in place.

Supports a key parameter for custom sorting criteria.

Also supports a key parameter for custom sorting criteria.

Requires additional memory for the new sorted list.

Performs the sorting in-place, saving memory.



sort() in Python

The sort function can be used to sort the list in both ascending and descending order. It can be used to sort lists of integers, floating point numbers, strings, and others in Python. Its time complexity is O(NlogN).

Similar Reads

Python sort() Syntax

The syntax of the sort() function in Python is as follows....

What is Python sort() Function?

In Python, the sort() function is a method that belongs to the list . It is used to sort in python or the elements of a list in ascending order by default. The sort() method modifies the original list in-place, meaning it rearranges the elements directly within the existing list object, rather than creating a new sorted list....

Sort() in Python Examples

A basic example of Python sort() method....

Different Ways to Sort() in Python

...

Difference between sorted() and sort() function in Python

In Python, sort() is a built-in method used to sort elements in a list in ascending order. It modifies the original list in place, meaning it reorders the elements directly within the list without creating a new list. The sort() method does not return any value; it simply sorts the list and updates it....