Python List sort() Method Tutorial

In this section, we will learn what the List sort() method is and how to use it in Python.

How to Sort a List in Python? (Python sort() Method

The Python List sort() method is used to sort the elements of a list in an ascending order by default or descending order if we set a specific argument into the method.

List sort() Method Syntax:

list.sort(reverse = True | False, key = funcionName)

List sort() Method Parameter:

The method has two optional parameters:

  • `reverse`: this parameter takes a boolean argument and if it was True, it means the order of the elements in the target list should be in descending. The default value is False which means the elements should be ordered in an ascending mode by default.
  • `key`: this parameter takes a pointer to a function. When the elements of a list are of complicated type (for example they are dictionary or a custom made object etc.), we can’t just run the sort() method on the elements and expect a correct order! In a situation like this, we can pass a function as the argument for the `key` parameter and the `sort` method will pass each element of the list to that function. Basically, the `sort` method will invoke the target function per each element of the list and pass each element as its argument. After that, it is expected that the function returns a basic data type (like an integer or a string value etc.) Basically, something that sort() method could run the ordering operation properly on the results.

For example, if the elements of the target list are dictionaries and they all have a property named `age` with an integer number as its value, and we want to order the elements of the list based on the value assigned to the `age` property, then we can create a function, pass that as the argument for the `key` parameter, and then within the body of the function, return the value of the `age` property as a result. After that, the `sort` function knows how to order the results based on the integer numbers it gets from the function.

List sort() Method Return Value:

The sort() method does not return any value.

Ascending and Descending Order

As mentioned before, the `sort()` method is capable of ordering the elements of a list in ascending and descending orders.

Now let’s see how ascending and descending orders can be done using the sort() method.

Example: ascending order python list

names = [
    {"name":"Jack", "age":500},
    {"name":"Elon", "age": 100},
    {"name": "Omid", "age": 3000}, 
    {"name": "John", "age": 700}
]

def orderList(element):
    return element["age"];

names.sort(reverse= False, key = orderList)

print(names)

Output:

[{'name': 'Elon', 'age': 100}, {'name': 'Jack', 'age': 500}, {'name': 'John', 'age': 700}, {'name': 'Omid', 'age': 3000}]

Here, the ordering is in ascending mode and it’s based on the value assigned to the `age` variable.

Example: descending order python list

names = [
    {"name":"Jack", "age":500},
    {"name":"Elon", "age": 100},
    {"name": "Omid", "age": 3000}, 
    {"name": "John", "age": 700}
]

def orderList(element):
    return element["age"];

names.sort(reverse= True, key = orderList)

print(names)

Output:

[{'name': 'Omid', 'age': 3000}, {'name': 'John', 'age': 700}, {'name': 'Jack', 'age': 500}, {'name': 'Elon', 'age': 100}]

Leave a Reply