Python List sort Function

How to use this Python List sort function with practical examples?. The Python sort List function is a List function used to sort a given list in ascending order. The syntax behind this Python sort List function is

# Python Sort List Function Syntax

# Sort List Items in Ascending Order
list_Name.sort()

# Sort List Items in Descending Order or Reverse Order
list_Name.sort(reverse = True)

# Sort Liste Items using Built-in or User defined Function
list_Name.sort(key = Function_Name)

Within this Python list sort function syntax, Function_Name can be any user-defined function. Or you can use any built-in function like len, min, max, etc., inside this Python sort list function.

Python sort List of Lists Examples

The following Python examples show you how to sort integer list items in ascending and Descending order.

Python sort List of Lists Example 1

The python sort list function sorts the given list in ascending order. The below code sorts the elements in an integer list.

# Python Sort List Function

sortList = [10, 190, 120, 180, 120, 105]

sortList.sort()

print("The Python Sorted List Items are : ", sortList)

List sort function output

The Python Sorted List Items are :  [10, 105, 120, 120, 180, 190]

sort List of Integers Example 2

The sort function has one argument called reverse = True. It allows us to sort the list items in descending order. The following code sorts the integer list elements in Descending order or reverse order.

TIP: Please refer to List article and List methods article to understand everything about Python Lists. And also refer to lenmin, and max.

# Python Sort List Example

listexm = [13, 98, 125, 191, 22, 9, 91, 78, 242]
print("\n Original List = ", listexm)

listexm.sort()
print("The Sorted List in Ascending = ", listexm)

listexm2 = [13, 98, 125, 191, 22, 9, 91, 78, 242]
print("\n Original List listexm2 = ", listexm2)

listexm2.sort(reverse = True)
print("The Sorted List in Descending = ", listexm2)

List sort function output


 Original List =  [13, 98, 125, 191, 22, 9, 91, 78, 242]
The Sorted List in Ascending =  [9, 13, 22, 78, 91, 98, 125, 191, 242]

 Original List listexm2 =  [13, 98, 125, 191, 22, 9, 91, 78, 242]
The Sorted List in Descending =  [242, 191, 125, 98, 91, 78, 22, 13, 9]

Python sort List of Strings Examples

This section shows how to sort string list items in ascending and descending order.

Python sort List of strings Example 1

In this example, first, we declared a string list. Next, we used python list sort function to sort them in ascending order. Here, sort function uses alphabetical order to sort the string list.

# Python Sort List Function

FruitList = ['Orange', 'Banana', 'Watermelon', 'Kiwi', 'Grape', 'Blackberry']

FruitList.sort()

print("The Python Sorted List Items are : \n", FruitList)
The Python Sorted List Items are : 
 ['Banana', 'Blackberry', 'Grape', 'Kiwi', 'Orange', 'Watermelon']

sort List in Descending Order Example

In this example, we used the reverse = True option inside the Python sort function. It means this example returns the reverse order of the above example.

# Python Sort List Example

fruitlist = ['orange', 'banana', 'apple', 'kiwi', 'grape']
print("\n Original List = ", fruitlist)

fruitlist.sort()
print("The Sorted List in Ascending = ", fruitlist)

# Sort Python List in Descending Order
fruitlist.sort(reverse = True)
print("The Sorted List in Descending = ", fruitlist)

List sort function to sort list items in descending order output


 Original List =  ['orange', 'banana', 'apple', 'kiwi', 'grape']
The Sorted List in Ascending =  ['apple', 'banana', 'grape', 'kiwi', 'orange']
The Sorted List in Descending =  ['orange', 'kiwi', 'grape', 'banana', 'apple']

Python sort List Function Example 3

The sort function on list items has another argument called a key. It accepts any function to determine the sorting factor. In this example, we created a user-defined function called length. It finds the length of a given string. Next, we are using this sort list function as the key value. It means, fruits list sorted based on the length in the ascending order.

In the next line, we used the reverse argument, along with this key argument. It sorts the list of the fruits based on each word length in descending order.

# Python Sort List Example

def length(str):
    return len(str)

fruitlist = ['apple', 'oranges', 'blackberry', 'kiwi', 'cherry']
print("\n Original List = ", fruitlist)

fruitlist.sort(key = length)
print("Sorted List in Ascending = ", fruitlist)

# Sort Python List in Descending Order
fruitlist.sort(reverse = True, key = length)
print("Sorted List in Descending= ", fruitlist)

 Original List =  ['apple', 'oranges', 'blackberry', 'kiwi', 'cherry']
Sorted List in Ascending =  ['kiwi', 'apple', 'cherry', 'oranges', 'blackberry']
Sorted List in Descending=  ['blackberry', 'oranges', 'cherry', 'apple', 'kiwi']

Python sort List of strings Example 4

To use the key argument, you don’t have to create a custom function. Python allows you to use any built-in functions.

This sort list example is the same as the above example. However, we are using the built-in common len function. It means the first one sort the list based on word length in ascending — the second one sort in Descending order using their length.

# Python Sort List Example

fruitlist = ['oranges', 'blackberry', 'apple', 'kiwi', 'cherry']
print("\n Original List = ", fruitlist)

fruitlist.sort(key = len)
print("Sorted List in Ascending = ", fruitlist)

# Sort Python List in Descending Order
fruitlist.sort(reverse = True, key = len)
print("Sorted List in Descending= ", fruitlist)

List sort function output


 Original List =  ['oranges', 'blackberry', 'apple', 'kiwi', 'cherry']
Sorted List in Ascending =  ['kiwi', 'apple', 'cherry', 'oranges', 'blackberry']
Sorted List in Descending=  ['blackberry', 'oranges', 'cherry', 'apple', 'kiwi']

Python sort List of Mixed List Example

Let me use this sort function on Mixed List.

# Python Sort List Function

MixedSortList = ['apple',  1, 5, 'Kiwi', 'Mango']

MixedSortList.sort()

print("The Python Sorted List Items are : \n", MixedSortList)

It is throwing an error because it can’t apply < operation on string and int

Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 3, in <module>
    MixedSortList.sort()
TypeError: '<' not supported between instances of 'int' and 'str'
>>> 

Python sort Nested List Examples

The following examples help you understand the sort function on the nested list items.

Python sort Nested List Example 1

This time, we used list sort function on the Nested list (list inside a list). Here, the sort function uses the first value in each nested list to sort items in ascending order.

# Python Sort List Function

MixedSortList = [[71, 222], [222, 13], [14, 15], [99, 77]]

MixedSortList.sort()

print("The Python Sorted List Items are : \n", MixedSortList)

sort function on nested list items output

The Python Sorted List Items are : 
 [[14, 15], [71, 222], [99, 77], [222, 13]]

List sort Example 2

You don’t have to restrict yourself by sorting the nested list with the first value. By creating a user-defined function, you can use any value to sort the nested lists.

In this example, we created a function to select the second value as the key to sorting the list. It means the first code snippet sort the list items based on the second value in a nested list — the second line sorts in descending order using the second item.

 # Python Sort List Example

def second(value):
    return value[1]

mixedlist = [[17, 222], [222, 13], [14, 151], [99, 77]]
print("\n Original List = ", mixedlist)

mixedlist.sort(key = second)
print("\nAscending Sorted List = ", mixedlist)

# Sort Python List in Descending Order
mixedlist.sort(reverse = True, key = second)
print("Descending Sorted List = ", mixedlist)

List sort function output


 Original List =  [[17, 222], [222, 13], [14, 151], [99, 77]]

Ascending Sorted List =  [[222, 13], [99, 77], [14, 151], [17, 222]]
Descending Sorted List =  [[17, 222], [14, 151], [99, 77], [222, 13]]

Python sort Nested List Example 3

We have taken another example to explain this key argument and custom functions. This time also we used second value a the sorting key. As you can see, though, we have three values in a nested list, it was sorting the list items by the second item.

 # Python Sort List Example

def second(value):
    return value[1]

mixedlist = [[17, 222, 15], [222, 13, 55], [14, 151, 31], [99, 77, 9]]
print("\n Original List = ", mixedlist)

mixedlist.sort(key = second)
print("\nAscending List = ", mixedlist)

# Sort Python List in Descending Order
mixedlist.sort(reverse = True, key = second)
print("Descending List = ", mixedlist)

sort function on nested list items output


 Original List =  [[17, 222, 15], [222, 13, 55], [14, 151, 31], [99, 77, 9]]

Ascending List =  [[222, 13, 55], [99, 77, 9], [14, 151, 31], [17, 222, 15]]
Descending List =  [[17, 222, 15], [14, 151, 31], [99, 77, 9], [222, 13, 55]]

sort Nested List Example 4

Let me change this key value to 3 nested list values. We can do by changing the item index position to 2 in a user-defined function.

 # Python Sort List Example

def third(value):
    return value[2]

mixedlist = [[17, 222, 15], [222, 13, 55], [14, 151, 31], [99, 77, 9]]
print("\n Original List = ", mixedlist)

mixedlist.sort(key = third)
print("\nAscending List = ", mixedlist)

# Sort Python List in Descending Order
mixedlist.sort(reverse = True, key = third)
print("Descending List = ", mixedlist)

It is sorting the list by using the third nested list item.


 Original List =  [[17, 222, 15], [222, 13, 55], [14, 151, 31], [99, 77, 9]]

Ascending List =  [[99, 77, 9], [17, 222, 15], [14, 151, 31], [222, 13, 55]]
Descending List =  [[222, 13, 55], [14, 151, 31], [17, 222, 15], [99, 77, 9]]

List sort Function on Dynamic Integer List Example

This python program is the same as the first example. However, this time we are allowing the user to enter the length of a List. Next, we used For Loop to add numbers to the list. Lastly, we used the Python list sort function to sort the integer list in ascending order.

# Python Sort List Function

intSortList = []

number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intSortList.append(value)

print("The Python List Before Sorting is : ", intSortList)
intSortList.sort()
print("The Python Sorted List is : ", intSortList)
Python Sort List Function 5

List sort Function Example 2

In this list sort program, we used the reverse argument to sort the list of the Descending order.

 # Python Sort List Example

numList = []

number = int(input("Please enter the Number of List Elements: "))
for i in range(1, number + 1):
    item = int(input("Please enter the Value of %d Element : " %i))
    numList.append(item)

print("The Python List Before Sorting is : ", numList)
numList.sort(reverse = True)
print("Sorted List in Descending : ", numList)

List sort function output

Please enter the Number of List Elements: 5
Please enter the Value of 1 Element : 25
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 12
Please enter the Value of 4 Element : 87
Please enter the Value of 5 Element : 20
The Python List Before Sorting is :  [25, 33, 12, 87, 20]
Sorted List in Descending :  [87, 33, 25, 20, 12]

sort List Function on Dynamic String List Example

This sort list program allows users to enter their own string or words. Next, we used the list sort function to sort them in ascending order.

# Python Sort List Function

strSortList = []

number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    item = input("Please enter the Value of %d Element : " %i)
    strSortList.append(item)

print("The Python List Before Sorting is : ", strSortList)
strSortList.sort()
print("The Python Sorted List is : ", strSortList)

List sort function output

Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : kiwi
Please enter the Value of 2 Element : dragon
Please enter the Value of 3 Element : apple
Please enter the Value of 4 Element : banana
The Python List Before Sorting is :  ['kiwi', 'dragon', 'apple', 'banana']
The Python Sorted List is :  ['apple', 'banana', 'dragon', 'kiwi']

sort List Function Example 2

In this list sorting program, we used the reverse argument inside a function to sort the string item sin descending order. Next, we used the key = len, to sort the list item sin ascending and Descending order using the string length.

 # Python Sort List Example

strList = []

number = int(input("Please enter the Total List Elements: "))
for i in range(1, number + 1):
    item = input("Please enter the Value of %d Element : " %i)
    strList.append(item)

print("List Before Sorting : ", strList)
strList.sort(reverse = True)
print("Sorted List in Descending : ", strList)

strList.sort(reverse = True, key = len)
print("Sorted List by Length : ", strList)

List sort function output.

Please enter the Total List Elements: 5
Please enter the Value of 1 Element : cherry
Please enter the Value of 2 Element : apple
Please enter the Value of 3 Element : orange
Please enter the Value of 4 Element : kiwi
Please enter the Value of 5 Element : balckberry
List Before Sorting :  ['cherry', 'apple', 'orange', 'kiwi', 'balckberry']
Sorted List in Descending :  ['orange', 'kiwi', 'cherry', 'balckberry', 'apple']
Sorted List by Length :  ['balckberry', 'orange', 'cherry', 'apple', 'kiwi']