Skip to content
On this page

Python List sort()

In Python, a list is an ordered collection of elements, and sorting a list is a common operation in programming. The sort() method is a built-in function available for lists in Python, which allows you to sort the elements of a list in place. Sorting a list arranges its elements in ascending or descending order based on the specified criteria.

Syntax - sort()

The sort() method has a simple syntax:

python
list.sort(key=None, reverse=False)
  • key: (Optional) A function that determines the sort order. You can specify a custom function to control the sorting behavior. If not provided, the list is sorted based on the natural order of its elements.
  • reverse: (Optional) If set to True, the list will be sorted in descending order. By default, it is set to False, which means the list will be sorted in ascending order.

Sort a list in ascending order

By default, the sort() method will sort the list in ascending order, meaning the elements will be arranged from the smallest to the largest value.

python
# Example of sorting a list in ascending order
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort()
print(numbers)

Output:

[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

Sort a list in descending order

To sort a list in descending order, you can set the reverse parameter to True while using the sort() method.

python
# Example of sorting a list in descending order
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort(reverse=True)
print(numbers)

Output:

[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

Sort a list with a custom function

You can also sort a list based on a custom function that defines the sorting logic. The key parameter in the sort() method allows you to specify a function that takes an element as input and returns a value by which the elements will be sorted.

python
# Example of sorting a list of strings based on their lengths
fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
fruits.sort(key=len)
print(fruits)

Output:

['kiwi', 'apple', 'cherry', 'banana', 'orange']

In this example, we sorted the list of fruits based on their lengths using the len() function as the key.

Sort a list of dictionaries

When dealing with a list of dictionaries, you can use the sort() method with a custom function and the key parameter to specify a particular key within the dictionaries to use for sorting.

python
# Example of sorting a list of dictionaries based on the 'age' key
people = [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25},
    {'name': 'Charlie', 'age': 35},
    {'name': 'David', 'age': 22}
]

people.sort(key=lambda x: x['age'])
print(people)

Output:

[
    {'name': 'David', 'age': 22},
    {'name': 'Bob', 'age': 25},
    {'name': 'Alice', 'age': 30},
    {'name': 'Charlie', 'age': 35}
]

In this example, we sorted the list of dictionaries based on the 'age' key using a lambda function as the key.

Keep in mind that the sort() method modifies the original list in place. If you want to create a sorted copy of the list without modifying the original, you can use the sorted() function instead.

Using the sorted() function

The sorted() function in Python allows you to create a new sorted list from an iterable without modifying the original one. It works similarly to the sort() method, but instead of sorting the list in-place, it returns a new sorted list. Let's add an example using sorted() to sort a list:

python
# Example of using sorted() to sort a list
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers)
print("Original list:", numbers)
print("Sorted list:", sorted_numbers)

Output:

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

In this example, we used the sorted() function to create a new sorted list sorted_numbers from the original list numbers. The original list remains unchanged, and the sorted list is stored in a new variable.

Similarly, you can use the reverse and key parameters with sorted() to sort the list in descending order or based on a custom function, just like we did with the sort() method. Here's an example of using sorted() with a custom function:

python
# Example of using sorted() with a custom function
fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
sorted_fruits = sorted(fruits, key=lambda x: len(x), reverse=True)
print("Original list:", fruits)
print("Sorted list:", sorted_fruits)

Output:

Original list: ['apple', 'banana', 'cherry', 'orange', 'kiwi']
Sorted list: ['banana', 'orange', 'cherry', 'apple', 'kiwi']

In this example, we sorted the list of fruits based on their lengths in descending order using the len() function as the key with reverse=True. The original list fruits remains unchanged, and the sorted list is stored in sorted_fruits.