Python List - Sorting - list.sort()


Python - Sort List

To sort a python list in ascending or descending order, you can use sort() method of List class.

In this tutorial, we shall use sort() function and sort the given list in ascending or descending order.


Syntax of list.sort()

The syntax of sort() method is:

mylist.sort(*, key=None, reverse=False)

where

  • key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).
  • reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.

Examples

1. Sort a list in ascending order

To sort a list in ascending order, you just have to call sort() method on the list without any arguments. In the following example, we have a list of numbers. We will arrange the items of this list in ascending order.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]

mylist.sort()

print(mylist)

Output

[5, 8, 21, 21, 52, 52, 87]

Explanation

  1. The list starts as [21, 5, 8, 52, 21, 87, 52].
  2. The sort() method is called without arguments, so the default behavior arranges the list in ascending order.
  3. The list is updated in place to [5, 8, 21, 21, 52, 52, 87].

2. Sort a list in descending order

To sort a list in descending order, you can pass reverse=True argument to the sort() function.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]

mylist.sort(reverse=True)

print(mylist)

Output

[87, 52, 52, 21, 21, 8, 5]

Explanation

  1. The list starts as [21, 5, 8, 52, 21, 87, 52].
  2. The sort() method is called with reverse=True, arranging the list in descending order.
  3. The list is updated in place to [87, 52, 52, 21, 21, 8, 5].

3. Sort a list of strings by length

In this example, we will sort a list of strings based on their length using the key parameter.

Python Program

mylist = ['apple', 'kiwi', 'banana', 'cherry']

mylist.sort(key=len)

print(mylist)

Output

['kiwi', 'apple', 'cherry', 'banana']

Explanation

  1. The list starts as ['apple', 'kiwi', 'banana', 'cherry'].
  2. The sort() method is called with key=len, sorting the strings by their length.
  3. The sorted list is ['kiwi', 'apple', 'cherry', 'banana'].

4. Sort a list of tuples by the second element

This example demonstrates sorting a list of tuples based on the second element of each tuple.

Python Program

mylist = [(1, 'b'), (3, 'a'), (2, 'c')]

mylist.sort(key=lambda x: x[1])

print(mylist)

Output

[(3, 'a'), (1, 'b'), (2, 'c')]

Explanation

  1. The list starts as [(1, 'b'), (3, 'a'), (2, 'c')].
  2. The sort() method is called with a lambda function that extracts the second element of each tuple for sorting.
  3. The sorted list becomes [(3, 'a'), (1, 'b'), (2, 'c')].

5. Sort a list using case-insensitive comparison

Sort a list of strings alphabetically, ignoring case.

Python Program

mylist = ['banana', 'Apple', 'cherry', 'apple']

mylist.sort(key=str.lower)

print(mylist)

Output

['Apple', 'apple', 'banana', 'cherry']

Explanation

  1. The list starts as ['banana', 'Apple', 'cherry', 'apple'].
  2. The sort() method is called with key=str.lower, ignoring case when comparing strings.
  3. The sorted list is ['Apple', 'apple', 'banana', 'cherry'].

Summary

In this tutorial of Python Examples, we learned how to sort a Python list, with the help of well-detailed example programs.




Python Libraries