Python List sort() Method

The sort() method is a built-in Python list method that sorts the elements of the list in-place, modifying the original list. By default, it sorts the elements in ascending order.

Syntax:

list.sort(key=None, reverse=False)

Parameters:

  • key (Optional): A function that serves as a key for the sort comparison. The value returned by this function for each element is used for the sorting process.
  • reverse (optional): A boolean value. If set to True, the list elements are sorted in descending order. Default is False.

Return Value:

The sort() method returns None. It modifies the original list in-place.

Example 1: Sorting a list of integers

numbers = [10, 40, 30, 20]

numbers.sort()

print(numbers)

# Output: [10, 20, 30, 40]

Example 2: Sorting a list of strings

The sort() method sorts the list of strings in alphabetical order.

colors = ["red", "green", "orange", "blue"]

colors.sort()

print(colors)

# Output: ['blue', 'green', 'orange', 'red']

When sorting a list of strings in Python, be mindful of case sensitivity as it can affect the sorting order. By default, Python sorts strings in case-sensitive order, meaning that uppercase letters come before lowercase letters.

Sorting in descending order

You can sort the elements of the list in descending order by setting the reverse parameter of the sort() method to True.

Here’s an example:

numbers = [10, 40, 30, 20]

numbers.sort(reverse=True)

print(numbers)

# Output: [40, 30, 20, 10]

Sorting based on a custom sorting function

You can define your own function to determine the sorting order and pass this function as the key argument to the sort() method. This function will be applied to each element of the list, and the list will be sorted based on the values returned by this function.

Here’s an example that sorts the elements of the list based on their lengths:

# Define a custom sorting function
def custom_sort(item):
    return len(item)

# List to be sorted
my_list = ["moon", "galaxy", "sun", "stars"]

# Sort the list using the custom sorting function
my_list.sort(key=custom_sort)

print(my_list)

# Output: ['sun', 'moon', 'stars', 'galaxy']

In this example, the custom_sort() function takes an item from the list as an argument, and returns the length of the string based on which the sorting will be performed. So the list is sorted based on the length of the strings.

You can modify the custom_sort() function to implement any custom sorting logic you need, such as sorting based on a specific attribute of an object, sorting by multiple criteria, or applying complex sorting algorithms.

Example: Sort a list of strings by length in descending order

# Define a custom sorting function
def custom_sort(item):
    return len(item)

# List to be sorted
my_list = ["moon", "galaxy", "sun", "stars"]

# Sort the list using the custom sorting function in descending order
my_list.sort(key=custom_sort, reverse=True)

print(my_list)

# Output: ['galaxy', 'stars', 'moon', 'sun']