Python List sort()

Python List sort() method

Python List sort() method sorts the items in the list in place in ascending order.

You can also specify a custom key to the sort() method to sort the items based on a function.

In this tutorial, you will learn the syntax of, and how to use List sort() method, with examples.

Syntax of List sort()

The syntax of List sort() method is

list.sort(iterable)

You can read the above expression as list extended with elements from the iterable.

Parameters

sort() method can take two named parameters. Let us see the parameters, and their description.

ParameterDescription
keyOptional
The key specifies a function of one argument that is used to extract a comparison key from each item of the list.
For example key=str.lower specifies that the comparison between the items in list be made with their lowercase.
You can also specify a custom function that you defined with one parameter, and that returns a value.
The default value is None (compare the elements directly).
reverseOptional
A boolean value.
If set to True, the list is sorted in descending order.
The default value is False.

Return value

extend() method returns None.

Please note that the extend() method modifies the original list.

Examples

1. Sort numbers in a list in ascending order in Python

In the following program, we take a list my_list with numeric values. We have to sort this list of numbers in ascending order.

Call sort() method on the list my_list, and pass no arguments.

Python Program

my_list = [5, 3, 1, 6, 2, 4]
print(f"Original list : {my_list}")

my_list.sort()
print(f"Sorted list   : {my_list}")
Run Code Copy

Output

Original list : [5, 3, 1, 6, 2, 4]
Sorted list   : [1, 2, 3, 4, 5, 6]

my_list is sorted in ascending order.

2. Sort list in descending order in Python

Let us take the same list my_list as in previous example, and we have to sort the items in the list in descending order.

Call sort() method on the list my_list, and pass True for reverse named parameter.

Python Program

my_list = [5, 3, 1, 6, 2, 4]
print(f"Original list : {my_list}")

my_list.sort(reverse=True)
print(f"Sorted list   : {my_list}")
Run Code Copy

Output

Original list : [5, 3, 1, 6, 2, 4]
Sorted list   : [6, 5, 4, 3, 2, 1]

my_list is sorted in descending order.

3. Sort list of strings lexicographically in Python

In the following program, we take a list of strings in my_list. We have to lexicographically sort this list in ascending order. That is, we have to sort the string based on the order they appear in a typical language dictionary.

Call sort() method on the list my_list, and pass no arguments. When the strings are compared for sorting, they are sorted based on their actual values, and that sorts them lexicographically, by default.

Python Program

my_list = ['cherry', 'apple', 'mango', 'banana']
print(f"Original list : {my_list}")

my_list.sort()
print(f"Sorted list   : {my_list}")
Run Code Copy

Output

Original list : ['cherry', 'apple', 'mango', 'banana']
Sorted list   : ['apple', 'banana', 'cherry', 'mango']

4. Sort list based on a funciton in Python

In the following program, we take a list of strings in my_list. We have to sort this list in ascending order based on the length of the items. That is, the string item with length of 3 is less than that of a string with length of 4.

Let us define a function, that takes a string as argument, and returns the length of the string. Let us name this function as my_function. Call sort() method on the list my_list, and pass the my_function for the key named parameter.

Python Program

def my_function(x):
    return len(x)

my_list = ['banana', 'watermelon', 'fig', 'apple']
print(f"Original list : {my_list}")

my_list.sort(key=my_function)
print(f"Sorted list   : {my_list}")
Run Code Copy

Output

Original list : ['banana', 'watermelon', 'fig', 'apple']
Sorted list   : ['fig', 'apple', 'banana', 'watermelon']

The list is sorted based on the string length.

Summary

In this tutorial of Python Examples, we learned about List sort() method, how to use sort() method to sort a given list, with syntax and examples.

We have seen examples for how to sort a list of numbers, how to sort a list of numbers in descending order, how to lexicographically sort a list of strings, how to sort a list based on a custom key function.

Related Tutorials

Code copied to clipboard successfully 👍