Open In App

Python List sort() Method

Python list sort() method sorts the elements of a list. It sorts in ascending order by default but can also sort values in descending order or in a custom manner using its parameters.

Example: Python list sort alphabetically and numerically.






alphabets = ['a','e','d','c','b']
alphabets.sort()
print(alphabets)
  
random_numbers = [2,5,6,1,8,3]
random_numbers.sort()
print(random_numbers)

Output:

['a', 'b', 'c', 'd', 'e']
[1, 2, 3, 5, 6, 8]

Python List sort() Syntax

List_name.sort(reverse=True/False, key=myFunc)



Parameters:

Return:

Python list sort() returns none.

What is the List sort() Method?

list sort() function is an in-built function in Python, that is used to sort the values of a list in ascending or descending order. By default it sorts values in ascending order. Python list sort time complexity is O(nlogn).

It is a very useful and simple list operation. It changes the original list instead of returning the new ones.

How to Use List sort() Function

Using the list sort() function in Python is very easy. You just need to call the function with the list object. You can use the parameters if you want but it is not mandatory to use.

Note: The difference between sort() and sorted() is that the sort list in Python alters the list directly and produces no output, whereas sorted() doesn’t change the list and returns the sorted list.

Python list sort() Examples and Use 

Let’s see different use case scenarios of list sort() method with examples. Below are some of them:

  1. Sort a List of Numbers in Ascending Order
  2. Sort a List of Alphabets In Ascending Order
  3. Sort a List in Python in Descending Order 
  4. Sort a List in Python By Key 

Python List sort numbers in Ascending Order

The sort() method by default sorts elements in ascending order as we can see below example:




numbers = [1, 3, 4, 2]
# Sorting list of Integers in ascending
print(numbers.sort())
print(numbers)

Output

None
[1, 2, 3, 4]

Sort a List of Alphabets In Ascending Order

The sort() method sorts the list in order from A-Z or a-z in the alphabet. 




strs = ["geeks", "code", "ide", "practice"]
strs.sort()
print(strs)

Output

['code', 'geeks', 'ide', 'practice']

Python Sort List in Descending Order 

Here, we are sorting the list of numbers in Descending order, the same will be for alphabets(Z-A, z-a). To do this we need to pass reverse=True, this will sort numbers or the alphabet in descending order.




numbers = [1, 3, 4, 2]
numbers.sort(reverse=True)
print(numbers)

Output

[4, 3, 2, 1]

Python sort List by Key 

In this example, we are sorting elements using the function based on passing the function to the key parameter of the sort() function.




def sortSecond(val):
    return val[1]
my_list1 = [(1, 2), (3, 3), (1, 1)]
my_list1.sort(key=sortSecond)
print(my_list1)
my_list1.sort(key=sortSecond, reverse=True)
print(my_list1)

Output

[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]

We have covered the definition, syntax, and examples of the list sort method in Python. It is a very useful and basic list function.

Read More Python List Methods

Also Read


Article Tags :