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() 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.

Python




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]

Similar Reads

Python List sort() Syntax

...

What is the List sort() Method?

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

How to Use List sort() Function

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

Python list sort() Examples and Use

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

Contact Us