Hit enter after type your search item

What is the Sort method in Python?

The List in Python has a built-in method to sort the list in ascending or descending order. The method is called sort() and 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

Example1 Example2 Example3

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

Read the section below for more details on 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 changes 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 original list then you may prefer using the sort over sorted method.
  • The default sorting is in ascending order as 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 a 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:

Python list sort

See online demo and code


 

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:

sort desc

See online demo and code


 

Note this line of code:

lst_sort.sort(reverse=True)

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

A Demo 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:

sort return

See online demo and code


 

You can see, 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:

sorted

See online demo and code


 

You can see, the new list is created with the sorted items of 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.

sort string

See online demo and code


 

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:

sort string desc

See online demo and code


 

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

Conclusion:

The 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.

 

This div height required for enabling the sticky sidebar