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).
Python sort() Syntax
The syntax of the sort() function in Python is as follows.
Syntax: list_name.sort(key=…, reverse=…)
Parameters:
By default, Python sort() doesn’t require any extra parameters and sorts the list in ascending order. However, it has two optional parameters:
- key: function that serves as a key for the sort comparison
- reverse: If true, the list is sorted in descending order.
Return value: The sort() does not return anything but alters the original list according to the passed parameter.
Sort() in Python Examples
A basic example of Python sort() method.
Python3
unsorted_list = [ 2 , 4 , 5 , 32 , 6 , 255 , 5 , 42 ]
unsorted_list.sort()
print ( "Now it is sorted:" , unsorted_list)
|
Output
Now it is sorted: [2, 4, 5, 5, 6, 32, 42, 255]
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.
- Sorting List in Ascending Order
- Sorting List in Descending Order
- Sort with custom function using key
- Difference between sorted() and sort() function in Python
Sorting List in Ascending Order
By default, the sort() in Python sorts a list in ascending order if we do not provide it with any parameters.
Python3
numbers = [ 1 , 3 , 4 , 2 ]
numbers.sort()
print (numbers)
decimalnumber = [ 2.01 , 2.00 , 3.67 , 3.28 , 1.68 ]
decimalnumber.sort()
print (decimalnumber)
words = [ "Geeks" , "For" , "Geeks" ]
words.sort()
print (words)
|
Output
[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']
Sorting List in Descending Order
To sort a list in descending order, set the reverse parameter to True of the sort() function in Python.
Python3
numbers = [ 1 , 3 , 4 , 2 ]
numbers.sort(reverse = True )
print (numbers)
decimalnumber = [ 2.01 , 2.00 , 3.67 , 3.28 , 1.68 ]
decimalnumber.sort(reverse = True )
print (decimalnumber)
words = [ "Geeks" , "For" , "Geeks" ]
words.sort(reverse = True )
print (words)
|
Output
[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']
Sort with Custom Function using Key
To sort the list according to the user’s choice, pass a function to the key parameter of the Python sort() function.
Python3
def sortSecond(val):
return val[ 1 ]
list1 = [( 1 , 2 ),( 3 , 3 ),( 1 , 1 )]
list1.sort(key = sortSecond)
print (list1)
list1.sort(key = sortSecond,reverse = True )
print (list1)
|
Output
[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]
Difference between sorted() and sort() function in Python
Let us see the difference between the sorted() and sort() function in Python:
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. |
It can only sort a list that contains only one type of value. |
It sorts the list in place. |
To know more please refer Python difference between the sorted() and sort() function.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Sep, 2023
Like Article
Save Article