Python List sort() Method


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.

Syntax of sort() 🔝

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.

Sorting a List of Strings 🔝

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.

Sorting Numbers in Ascending 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]

Sorting in Descending Order 🔝

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.

Using the key Argument 🔝

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.

Sorting by Length in Descending Order

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']

Case-Insensitive String Sorting 🔝

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.

Sorting with a Custom Function 🔝

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.

Return Value of sort() 🔝

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]

Do Not Assign sort() Back to the List

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.

sort() vs reverse() 🔝

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.

sort() vs sorted() 🔝

The sort() method changes the original list. The sorted() function returns a new sorted list and leaves the original list unchanged.

Using sort()

my_list=[4, 2, 7, 1]

my_list.sort()

print(my_list)
Output
[1, 2, 4, 7]

Using sorted()

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.

Sorting Mixed Data Types 🔝

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.

Summary of sort() 🔝

  • sort() arranges the items of an existing list.
  • Ascending order is used by default.
  • Use reverse=True for descending order.
  • The key argument controls the value used for comparison.
  • key=len can sort strings by their length.
  • A custom function can also be supplied as the sorting key.
  • The method changes the original list in place.
  • The return value of sort() is None.
  • reverse() reverses the existing sequence but does not sort it.
  • sorted() returns a new sorted list without modifying the original.
  • Items must be comparable directly or through the supplied key.
All List Methods reverse() sorted() Questions with Solutions on List




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer