Python List sort() Method | Explained

Sorting plays an important part while analyzing or organizing the data more efficiently. The sorted data will be arranged either in increasing order or decreasing order. Python has a list data structure that stores a large collection of numeric or string data. To sort the list data, Python provides “sort()” and “sorted()” functions.

This write-up will give you detailed knowledge of the Python list sort() method with appropriate examples. The below-mentioned aspects will be elaborated in this Python blog post:

So let’s begin this guide!

What is the list.sort() Method in Python?

The “list.sort()” method is utilized in Python to sort the input items of the list according to ascending and descending order. The syntax of the “list.sort()” method is shown below:

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

In the above syntax:

  • The “sort()” method does not return any value; it only modifies the existing value.
  • The parameter “key” specifies the criteria for sorting.
  • The parameter “reverse” sorts the list in descending order if its value is set to “true”. 

Another built-in function, “sorted()”, is used to sort the list of elements. The syntax of the “sorted()” is shown below:

sorted(list, key=...., reverse=....)

In the above syntax:

  • The sorted() function takes the list variable as an argument and returns the copy of the sorted list in a new list.

Let’s start with the first example of the “sort()” method:

Example 1: Sorting of List in Ascending Order Using sort() Method

In the example given below, the “sort()” function sorts the numeric items of the given list according to ascending order.

Code:

# Create list of numbers
list_number = [22, 28, 3, 12, 13, 17, 1, 90, 44]

#sort list using sort()
list_number.sort()
print('sort of list in ascending order:\n',list_number)

In the above code:

  • The list of numbers is initialized in the program.
  • The “sort()” method sorts the input list according to ascending order if all parameter values are set to default.

Output:

The above output shows that the list of numbers has been sorted from lowest to highest.

Example 2: Sorting of List in Descending Order Using sort() Method

In the example given below, the “sort()” function takes the parameter value of “reverse” as a “True” to display the sorted list of strings in descending order.

Code:

# Create list of numbers
list_number = [22, 28, 3, 12, 13, 17, 1, 90, 44]

#sort list using sort()
list_number.sort(reverse=True)
print('sort of list in descending order:\n',list_number)

In the above code:

  • The “sort()” function takes the parameter “reverse=True” for sorting elements of the list in descending order.

Output:

The above output verified that the list of numbers had been sorted in descending order.

Example 3: Sorting of List by Key Parameter Using sort() Method

In the example given below, the sort() function sorts the elements of the list from lowest length to highest length using the parameter “key”.

Code:

list_string = ["Python", "Linux","Ubuntu", "Guide", 'Java']

list_string.sort(key=len)

print(list_string)

In the above code:

  • The list containing string values are initialized and stored in a variable named “list_string”.
  • The “sort()” function takes the value of parameter “key=len” and displays the sorted list according to the lowest length element to the highest length element.

Note: If you want to print from highest length to lowest, then you must use a parameter value “reverse=True” inside the parentheses of the “sort()” function.

Output:

The above output shows that the list of strings has been sorted from lowest string length to highest string length.

Example 4: Sorting of List Using sorted()

In the example shown below the “sorted()” function sorts the given list element according to ascending and descending order. Just like the “sort()” method, the “sorted()” function also takes the parameter values “key” and “reverse” as an argument.

Code:

# Create list of numbers
list_num = [30, 48, 43, 22, 54]

#sort list using sort()
sort_list = sorted(list_num,reverse=True)
print(sort_list)

In the above code:

  • The “sorted()” function takes the value of the list variable as an argument and returns the new sorted copy of the list.
  • In the above scenario, the list elements are sorted in descending order because the parameter “reverse” is set to “True”.

Output:

The above output shows that the list has been sorted in descending order.

That’s it from this Python Guide!

Conclusion

To sort the list elements according to ascending or descending order, the “sort()” method and “sorted()” function is used in Python. The “sort()” method returns nothing, while the “sorted()” function returns a copy of the new sorted list. The “sort()” method takes the parameter value of “reverse” as “True” or “False” to sort the items of the list in descending or ascending order. Similarly, the parameter “key” is defined to sort the elements of the list according to specific values, such as the length of the element. This Python guide presented a detailed overview of the list sort() method.