The Python sort() method arranges the items of a
list in a specified order. By default, values are sorted in ascending order.
The sort() method changes the original list in place. We can also use the optional key and reverse arguments to control how the values are sorted.
my_list.sort(key=None, reverse=False)
Both arguments are optional.
key - specifies a function that is applied to each item to determine its sorting value.reverse - when set to True, the list is sorted in descending order. The default value is False.
The method modifies the original list and returns None.
Strings are sorted according to their normal comparison order.
my_list=['Ronald', 'John', 'Alex', 'Ravi']
my_list.sort()
print(my_list)
Output
['Alex', 'John', 'Ravi', 'Ronald']
The original list is changed so that its string elements appear in sorted order.
For numbers, the default behavior is to arrange values from smallest to largest.
my_list=[4, 2, 7, 1]
my_list.sort()
print(my_list)
Output
[1, 2, 4, 7]
Set reverse=True to sort values in descending order.
my_list=[4, 2, 7, 1]
my_list.sort(reverse=True)
print(my_list)
Output
[7, 4, 2, 1]
Here, the values are arranged from highest to lowest.
The key argument lets us specify a function that determines the value used for sorting.
In this example, the len() function is used as the key, so the strings are sorted according to their length.
my_list=['ab', 'xa', 'dc', 'Cxx', 'yzza', 'Z', 'b']
my_list.sort(key=len)
print(my_list)
Output
['Z', 'b', 'ab', 'xa', 'dc', 'Cxx', 'yzza']
The shortest strings appear first. Strings with the same length keep their relative order from the original list.
We can combine key=len with reverse=True to place the longest strings first.
my_list=['ab', 'xa', 'dc', 'Cxx', 'yzza', 'Z', 'b']
my_list.sort(key=len, reverse=True)
print(my_list)
Output
['yzza', 'Cxx', 'ab', 'xa', 'dc', 'Z', 'b']
Uppercase and lowercase characters can affect the normal sorting order of strings. We can use str.lower as the key to compare the strings using lowercase values.
my_list=['banana', 'Apple', 'cherry', 'Mango']
my_list.sort(key=str.lower)
print(my_list)
Output
['Apple', 'banana', 'cherry', 'Mango']
The original capitalization is preserved. The lowercase versions are used only to determine the sorting order.
A user-defined function can also be supplied to the key argument.
In this example, the last character of each string is used for sorting.
def last_character(value):
return value[-1]
my_list=['Alex', 'John', 'Ravi', 'Ronald']
my_list.sort(key=last_character)
print(my_list)
Output
['Ronald', 'Ravi', 'John', 'Alex']
The function returns the last character of each string. The sort() method uses those returned characters as the values for comparison.
The sort() method changes the existing list in place and returns None.
my_list=[4, 2, 7, 1]
result=my_list.sort()
print(result)
print(my_list)
Output
None
[1, 2, 4, 7]
A common mistake is to assign the result of sort() back to the same variable.
my_list=[4, 2, 7, 1]
my_list=my_list.sort()
print(my_list)
Output
None
The method sorts the original list but returns None. Therefore, call sort() directly without assigning its result back to the list variable.
The sort() and reverse() methods perform different operations.
sort() arranges values according to their comparison order.
my_list=[4, 2, 9, 1]
my_list.sort()
print(my_list)
Output
[1, 2, 4, 9]
The reverse() method simply reverses the current sequence.
my_list=[4, 2, 9, 1]
my_list.reverse()
print(my_list)
Output
[1, 9, 2, 4]
Use sort(reverse=True) when you need descending sorted order. Use reverse() when you only need to flip the existing sequence.
The sort() method changes the original list. The
sorted() function returns a new sorted list and leaves the original list unchanged.
my_list=[4, 2, 7, 1]
my_list.sort()
print(my_list)
Output
[1, 2, 4, 7]
my_list=[4, 2, 7, 1]
new_list=sorted(my_list)
print(new_list)
print(my_list)
Output
[1, 2, 4, 7]
[4, 2, 7, 1]
Use sort() when the original list should be changed. Use sorted() when you need a new sorted list while preserving the original one.
The items being sorted must be comparable according to the selected key. For example, Python cannot directly order integers and strings together.
my_list=[10, '20', 5]
my_list.sort()
Sample Error
TypeError: '<' not supported between instances of 'str' and 'int'
If a list contains different data types, convert the values to a common comparable form or provide a suitable key function before sorting.
sort() arranges the items of an existing list.reverse=True for descending order.key argument controls the value used for comparison.key=len can sort strings by their length.sort() is None.reverse() reverses the existing sequence but does not sort it.sorted() returns a new sorted list without modifying the original.key.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.