What is the Sort Method in Python?

A visual tutorial on Python's list.sort() method. Learn the ins and outs of sorting lists, including in-place sorting, custom key functions, and stability.

The List in Python has a built-in method to sort the list in ascending or descending order. The method is called sort() and is only specific to the lists.

This is how you may use the list.sort() Python method for ascending order:

lst_sort = [9, 5, 7, 3, 1]  #A list with five items

lst_sort.sort()   #Sorting in ascending order

 

For descending order, you may use the reverse parameter with the true value:

lst_sort.sort(reverse=True)  #sorting in descending order

 

Note: The ‘T’ must be capital for True value. Similarly, if you use false, then F must be capital.

Read the section below for more details on the Python sort method.

A few important points about sorting

  • The sort() method is specific to the list type.
  • If you need to use the sorting on other iterables including lists, then use the sorted() method.
  • The sort method modifies the original list.
  • It will return the none (See an example with the code below to understand further).
  • If you want to keep the original list in place, then use the sorted() method. As it builds a new sorted list from the iterable.
  • The sort() method is a little more efficient. In case, you do not require the original list then you may prefer using the sort() over sorted() method.
  • The default sorting is in ascending order by using the sort() method.
  • You may use the reverse parameter for descending order. (See an example in the coming section with code).

An example of list sort method

For this example, I have created a list of five numeric items in random order. After that, the list.sort() method is used to sort the list in ascending order.

The list items are displayed before and after using the sort() method. See the code and output:

#A Demo of sort() method of list

lst_sort = [9, 5, 7, 3, 1]


print ("The list items before sort method:",lst_sort)

lst_sort.sort()

print ("The list items after sort method:",lst_sort)

Output:

The list items before sort method: [9, 5, 7, 3, 1]
The list items after sort method: [1, 3, 5, 7, 9]

You can see, the list is sorted from lower to bigger numbers.

Sorting list in descending order example

Using the same list as in above example and passing the reverse parameter with True value for sorting the list in descending order. Have a look:

#A Demo of sort() in desc order

lst_sort = [9, 5, 7, 3, 1]

print ("The list items before sort method:",lst_sort)

lst_sort.sort(reverse=True)

print ("The list items after sort method:",lst_sort)

Result:

The list items before sort method: [9, 5, 7, 3, 1]
The list items after sort method: [9, 7, 5, 3, 1]

Note this line of code:

lst_sort.sort(reverse=True)

You can see, the print function displays the list in descending order.

An example of showing returned value after using sort() method

As mentioned earlier, the list sort method returns None to avoid confusion. See this example where a list is created and its items are displayed.

After that, the sort() method is used and the list is assigned to a new list so it gets the returned value.

The new list is also displayed and see what it displays:

#A Demo of sort() method with return value

lst_sort = [10, 5, 55, 80, 99]

print ("The list items before sort method:",lst_sort)


lst_new = lst_sort.sort()

print ("The new list with returned result:",lst_new)

Output:

The list items before sort method: [10, 5, 55, 80, 99]
The new list with returned result: None

You can see that it displays None. So, if you want to keep the existing list in the same order and assign the sorted list to a new list then you have to use the sorted method as shown below:

#A Demo of sorted() method with return value

lst_sort = [10, 5, 55, 80, 99]

print ("The list items before sort method:",lst_sort)

lst_new = sorted(lst_sort)

print ("The new list with returned result with sorted method:",lst_new)

Output:

sorted

You can see that the new list is created with the sorted items of the existing list by using the sorted method.

An example of sorting list of strings

If your list contains string items, then the sort Python method will arrange the results in ascending order just like the numeric example. For this demo, I have created a list of strings that contains five color names. The list is displayed before and after using the sort method.

#A Demo sorting list of strings

String_lst_sort = ['Red', 'Green', 'Yellow', 'Black', 'White']

print ("The list items before sort method:",String_lst_sort)

String_lst_sort.sort()

print ("The list of string after sort:",String_lst_sort)

Output:

The list items before sort method: [‘Red’, ‘Green’, ‘Yellow’, ‘Black’, ‘White’]
The list of string after sort: [‘Black’, ‘Green’, ‘Red’, ‘White’, ‘Yellow’]

List of strings in descending order example

Again, by using the reverse=True, the list of string is sorted in descending order. So, the string starting with ‘z’ should come first and ‘a’ at the last. See the output and code:

#A Demo sorting list of strings in desc order

String_lst_sort = ['Red', 'Green', 'Yellow', 'Black', 'White']


print ("The list items before sort method:",String_lst_sort)

String_lst_sort.sort(reverse=True)

print ("The list in desc order:",String_lst_sort)

Output:

sort string desc

You can see that Yellow is displayed first while Black in the last.

Conclusion:

Python has a few built-in methods for sorting the lists.

If you want to maintain an existing list order then you should use the sorted method.

If the existing list order is not required then you should use the list.sort() method for a little more efficiency.

For other iterators, you may use the sorted method as such the sort() is specific to the lists.

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!