Python – Difference between sorted() and sort()

03 Jun, 2022
Sorting means rearranging a given sequence of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of the element in the respective data structure.For example: The below list of characters is sorted in increasing order of their ASCII values. That is, the character with lesser ASCII value will be placed first than the character with higher ASCII value. 
In Python, sorting any sequence is very easy as it provides in-built methods for sorting. Two such methods are sorted() and sort(). These two methods are used for sorting but are quite different in their own way. Let’s have a look at them one by one. 
sorted() method sorts the given sequence as well as set and dictionary(which is not a sequence) either in ascending order or in descending order(does unicode comparison for string char by char) and always returns the a sorted list. This method doesn’t effect the original sequence. 
Syntax: sorted(iteraable, key, reverse=False)affectParameters: Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted. Key(optional): A function that would serve as a key or a basis of sort comparison. Reverse(optional): If set True, then the iterable would be sorted in reverse (descending) order, by default it is set as False.Return Type: Returns a sorted list.  
Example 1: 
Python3
# Python program to demonstrate# sorted()  L = [1, 2, 3, 4, 5] print("Sorted list:")print(sorted(L)) print("\nReverse sorted list:")print(sorted(L, reverse = True)) print("\nOriginal list after sorting:")print(L)
Output: 
Sorted list:
[1, 2, 3, 4, 5]

Reverse sorted list:
[5, 4, 3, 2, 1]

Original list after sorting:
[1, 2, 3, 4, 5]
Example 2: Sorting different data types 
Python3
# Python program to demonstrate# sorted()  # Listx = ['q', 'w', 'r', 'e', 't', 'y']print(sorted(x))   # Tuplex = ('q', 'w', 'e', 'r', 't', 'y')print(sorted(x)) # String-sorted based on ASCII translationsx = "python"print(sorted(x))   # Dictionaryx = {'q':1, 'w':2, 'e':3, 'r':4, 't':5, 'y':6}print(sorted(x))   # Setx = {'q', 'w', 'e', 'r', 't', 'y'}print(sorted(x))
Output: 
['e', 'q', 'r', 't', 'w', 'y']
['e', 'q', 'r', 't', 'w', 'y']
['h', 'n', 'o', 'p', 't', 'y']
['e', 'q', 'r', 't', 'w', 'y']
['e', 'q', 'r', 't', 'w', 'y']
 
This optional parameter key takes a function as its value. This key function transforms each element before sorting, it takes the value and returns 1 value which is then used within sort instead of the original value.Example: Let’s suppose we want to sort a List of string according to its length. This can be done by passing the len() function as the value to the key parameter. Below is the implementation. 
Python3
# Python program to demonstrate# sorted()  L = ['aaaa', 'bbb', 'cc', 'd'] # sorted without key parameterprint(sorted(L))print() # sorted with key parameterprint(sorted(L, key = len))
Output: 
['aaaa', 'bbb', 'cc', 'd']

['d', 'cc', 'bbb', 'aaaa']
 
sort() function is very similar to sorted() but unlike sorted it returns nothing and makes changes to the original sequence. Moreover, sort() is a method of list class and can only be used with lists. 
Syntax: List_name.sort(key, reverse=False)Parameters: key: A function that serves as a key for the sort comparison. reverse: If true, the list is sorted in descending order.Return type: None  
Example 1: 
Python3
# Python program to demonstrate# sort()  # List of Integersnumbers = [1, 3, 4, 2]   # Sorting list of Integersnumbers.sort()   print(numbers)   # List of Floating point numbersdecimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]   # Sorting list of Floating point numbersdecimalnumber.sort()   print(decimalnumber)   # List of stringswords = ["Geeks", "For", "Geeks"]   # Sorting list of stringswords.sort()   print(words)
Output: 
[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']
Example 2: Sorting in reverse order. 
Python3
# Python program to demonstrate# sort()  # List of Integersnumbers = [1, 3, 4, 2]   # Sorting list of Integersnumbers.sort(reverse = True)   print(numbers)   # List of Floating point numbersdecimalnumber = [2.01, 2.00, 3.67, 3.28, 1.68]   # Sorting list of Floating point numbersdecimalnumber.sort(reverse = True)   print(decimalnumber)   # List of stringswords = ["Geeks", "For", "Geeks"]   # Sorting list of stringswords.sort(reverse = True)   print(words)
Output: 
[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']
Example 3: Using key parameter. 
Python3
# Python program to demonstrate sorting by user's# choice   # function to return the second element of the# two elements passed as the parameterdef sortSecond(val):    return val[1]    # list1 to demonstrate the use of sorting # using  second key list1 = [(1, 2), (3, 3), (1, 1)]   # sorts the array in ascending according to # second elementlist1.sort(key = sortSecond) print(list1)   # sorts the array in descending according to# second elementlist1.sort(key = sortSecond, reverse = True)print(list1)
Output: 
[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]
 Let us see the differences in a tabular form -:
Its syntax is :
sorted(iterable, key=key, reverse=reverse)
Its syntax is -:
list.sort(reverse=True|False, key=myFunc)
flashtoak
simranarora5sos
mayank007rawa
sweetyty
python-basics
Python-sort
Python-Sorted
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts