Python List sort() Method

The sort() method in Python is a built-in function used to sort the items of a list in ascending or descending order. This method sorts the list in-place, meaning it modifies the original list, and does not return a new list. The sorted list can be ascending or descending based on the optional parameters that are passed to the sort() method.

Syntax

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

Parameters

key – An optional parameter that specifies a function to determine the sort order. If not specified, the default value is None and the sort order is determined by the elements in the list.

reverse – An optional parameter that takes a Boolean value. If it is set to True, the sort order will be in descending order, and if it is set to False, the sort order will be in ascending order. The default value is False.

Examples

Example 1: Sorting a List in Ascending Order

Let’s consider a list of numbers and sort it in ascending order using the sort() method:

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort()
print(numbers)

In this example, we have created a list of numbers called numbers. Then, we have used the sort() method to sort the list in ascending order. The reverse parameter is not specified, so the default value of False is used, and the sort order is in ascending order. Finally, we have printed the sorted list, which will be [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9].

Example 2: Sorting a List in Descending Order

Let’s consider the same list of numbers and sort it in descending order using the sort() method:

numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
numbers.sort(reverse=True)
print(numbers)

In this example, we have used the same list of numbers numbers. Then, we have used the sort() method to sort the list in descending order. The reverse parameter is set to True, so the sort order will be in descending order. Finally, we have printed the sorted list, which will be [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1].

Example 3: Sorting a List of Strings

Let’s consider a list of strings and sort it in ascending order using the sort() method:

strings = ['apple', 'banana', 'cherry', 'date', 'elderberry']
strings.sort()
print(strings)

In this example, we have created a list of strings called strings. Then, we have used the sort() method to sort the list in ascending order. The reverse parameter is not specified, so the default value of False is used, and the sort order is in ascending order. Finally, we have printed the sorted list, which will be ['apple', 'banana', 'cherry', 'date', 'elderberry'].

Example 4: Sorting a List of Tuples

Let’s consider a list of tuples and sort it in ascending order based on the first element of each tuple using the sort() method:

def get_first_element(item):
    return item[0]
tuples = [(1, 'apple'), (2, 'banana'), (3, 'cherry'), (4, 'date'), (5, 'elderberry')]
tuples.sort(key=get_first_element)
print(tuples)

In this example, we have created a function called get_first_element that returns the first element of each tuple. Then, we have created a list of tuples called tuples. Finally, we have used the sort() method to sort the list based on the first element of each tuple. The key parameter is set to get_first_element, so the sort order is determined by the first element of each tuple. Finally, we have printed the sorted list, which will be [(1, 'apple'), (2, 'banana'), (3, 'cherry'), (4, 'date'), (5, 'elderberry')].

Conclusion

The sort() method in Python is a useful tool for sorting lists in ascending or descending order. It can also sort based on a custom function if the key parameter is set. Understanding how to use the sort() method can help you efficiently sort lists in your Python projects.

Leave a Reply

Your email address will not be published. Required fields are marked *