Python .sort()
The Python .sort() method is used to sort list elements in ascending or descending order. It modifies the original list instead of returning a new one, making it a fast and memory-efficient operation for in-place sorting.
Python .sort() Syntax
list.sort(key=None, reverse=False)
Parameters:
key(Optional): A function that acts as a sorting key.reverse(Optional): A boolean value. IfTrue, the list is sorted in descending order.
Return value:
Returns None because the sorting happens in place.
Example 1: Sorting Numbers Using Python .sort()
This example uses Python .sort() to sort the numbers list in ascending and descending order:
numbers = [5, 2, 9, 1, 7]numbers.sort()print("Ascending:", numbers)numbers.sort(reverse=True)print("Descending:", numbers)
Here is the output:
Ascending: [1, 2, 5, 7, 9]Descending: [9, 7, 5, 2, 1]
Example 2: Sorting Strings Using Python .sort()
This example uses Python .sort() to sort a list of strings in alphabetical order:
fruits = ["apple", "banana", "cherry", "date"]fruits.sort()print(fruits)
Here is the output:
['apple', 'banana', 'cherry', 'date']
Codebyte Example: Using Python .sort() with a Custom Key
This codebyte example uses Python .sort() with a custom key to sort a list of strings. The key parameter is set to len, so the list is sorted by word length instead of alphabetical order:
Frequently Asked Questions
1. What does list.sort() return in Python?
The list.sort() method returns None because it sorts the list in place, modifying the original list rather than creating a new one.
2. Why is sort() returning None?
sort() always returns None to signal that the original list has been modified directly. If you need a new sorted list without changing the original, use the sorted() function instead.
3. When you use sort() does it permanently change the list?
Yes, sort() permanently changes the order of elements in the list. The original order is lost unless you make a copy before sorting.
4. How to sort a list without using the sort() function?
You can use the built-in sorted() function to return a new sorted list, or implement custom sorting logic such as bubble sort, selection sort, or quicksort.
5. What is the difference between the sort() method and the sorted() function?
sort()is a method available only to lists, sorts in place, and returnsNone.sorted()is a built-in function that works with any iterable, returns a new sorted list, and leaves the original data unchanged.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours