Open In App
Related Articles

Python List sort() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

Python list sort() method sorts the elements of a list.

Example:

Python3




# using a list random numbers
random_numbers = [2,5,6,1,8,3]
# sorting list
random_numbers.sort()
# printing sorted number list
print(random_numbers)


Output

[1, 2, 3, 5, 6, 8]



Definition of sort() method in Python

Python list sort() method is used to sort a list in ascending, descending or user-defined way.

In each case, the time complexity is O(nlogn) in Python.

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.

List sort() Method Syntax

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

Parameters:

  • reverse (Optional): for reverse=True, it will sort the list descending. Default is reverse=False
  • key (Optional) – A function to specify the sorting criteria(s)

How to Sort Elements of Python List?

Using Python sort() function, you can sort elements of list.

Python3




# using list alphabets
alphabets = ['a','e','d','c','b']
# sorting list
alphabets.sort()
# printing sorted list
print(alphabets)


Output

['a', 'b', 'c', 'd', 'e']



More List sort() Method Examples 

Lets see different use case scenarios of list sort() method with examples:

1. Sort a List of Numbers in Ascending Order

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

Python3




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


Output

None
[1, 2, 3, 4]



2. Sort a List of Alphabets In Ascending Order

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

Python3




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


Output

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



3. Sort a 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.

Python3




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


Output

[4, 3, 2, 1]



4. Sort the 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.

Python3




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)]



5. Sort the list by the length of the values

Here we are sorting the element of the list according to the length of element in ascending order.

Python3




def myFunc(e):
  return len(e)
  
lang = ['Java', 'python', 'cpp', 'c']
  
lang.sort(key=myFunc)
print(lang)


Output

['c', 'cpp', 'Java', 'python']



We have covered definition, syntax and examples of sort() method in Python. Hope this helps in your Python journey!!

Also Read:


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 01 Dec, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials