|
1 | 1 | ---
|
2 | 2 | Title: '.sort()'
|
3 |
| -Description: 'Sorts the contents of the list it is called on.' |
| 3 | +Description: 'Sorts the elements of a list in ascending or descending order.' |
4 | 4 | Subjects:
|
5 |
| - - 'Data Science' |
6 | 5 | - 'Computer Science'
|
| 6 | + - 'Data Science' |
7 | 7 | Tags:
|
| 8 | + - 'Elements' |
8 | 9 | - 'Lists'
|
9 | 10 | - 'Methods'
|
| 11 | + - 'Python' |
10 | 12 | CatalogContent:
|
11 | 13 | - 'learn-python-3'
|
12 | 14 | - 'paths/data-science'
|
13 |
| - - 'paths/computer-science' |
14 | 15 | ---
|
15 | 16 |
|
16 |
| -The `.sort()` method sorts the contents of the list it is called on. |
| 17 | +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. |
| 18 | + |
| 19 | +## Python `.sort()` Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +list.sort(key=None, reverse=False) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `key` (Optional): A [function](https://www.codecademy.com/resources/docs/python/functions) that acts as a sorting key. |
| 28 | +- `reverse` (Optional): A boolean value. If `True`, the list is sorted in descending order. |
| 29 | + |
| 30 | +**Return value:** |
17 | 31 |
|
18 |
| -## Syntax |
| 32 | +Returns `None` because the sorting happens in place. |
| 33 | + |
| 34 | +## Example 1: Sorting Numbers Using Python `.sort()` |
| 35 | + |
| 36 | +This example uses Python `.sort()` to sort the `numbers` list in ascending and descending order: |
19 | 37 |
|
20 | 38 | ```py
|
21 |
| -list.sort() |
| 39 | +numbers = [5, 2, 9, 1, 7] |
| 40 | + |
| 41 | +numbers.sort() |
| 42 | +print("Ascending:", numbers) |
| 43 | + |
| 44 | +numbers.sort(reverse=True) |
| 45 | +print("Descending:", numbers) |
22 | 46 | ```
|
23 | 47 |
|
24 |
| -Numerical lists will be sorted in ascending order, and lists of strings will be sorted into alphabetical order. It modifies the original list, and has no return value. |
| 48 | +Here is the output: |
25 | 49 |
|
26 |
| -## Example |
| 50 | +```shell |
| 51 | +Ascending: [1, 2, 5, 7, 9] |
| 52 | +Descending: [9, 7, 5, 2, 1] |
| 53 | +``` |
| 54 | + |
| 55 | +## Example 2: Sorting Strings Using Python `.sort()` |
27 | 56 |
|
28 |
| -To sort a list called `exampleList`: |
| 57 | +This example uses Python `.sort()` to sort a list of [strings](https://www.codecademy.com/resources/docs/python/strings) in alphabetical order: |
29 | 58 |
|
30 |
| -```python |
31 |
| -name = ['c', 'h', 'l', 'o', 'e'] |
| 59 | +```py |
| 60 | +fruits = ["apple", "banana", "cherry", "date"] |
32 | 61 |
|
33 |
| -name.sort() |
| 62 | +fruits.sort() |
34 | 63 |
|
35 |
| -print(name) |
36 |
| -# Output: ['c', 'e', 'h', 'l', 'o'] |
| 64 | +print(fruits) |
37 | 65 | ```
|
38 | 66 |
|
39 |
| -## Codebyte Example |
| 67 | +Here is the output: |
40 | 68 |
|
41 |
| -To sort a list called `exampleList`: |
| 69 | +```shell |
| 70 | +['apple', 'banana', 'cherry', 'date'] |
| 71 | +``` |
| 72 | + |
| 73 | +## Codebyte Example: Using Python `.sort()` with a Custom Key |
| 74 | + |
| 75 | +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: |
42 | 76 |
|
43 | 77 | ```codebyte/python
|
44 |
| -exampleList = [4, 2, 1, 3] |
| 78 | +words = ["Python", "is", "amazing", "AI"] |
45 | 79 |
|
46 |
| -exampleList.sort() |
| 80 | +words.sort(key=len) |
47 | 81 |
|
48 |
| -print(exampleList) |
| 82 | +print(words) |
49 | 83 | ```
|
| 84 | + |
| 85 | +## Frequently Asked Questions |
| 86 | + |
| 87 | +### 1. What does `list.sort()` return in Python? |
| 88 | + |
| 89 | +The `list.sort()` method returns `None` because it sorts the list **in place**, modifying the original list rather than creating a new one. |
| 90 | + |
| 91 | +### 2. Why is `sort()` returning `None`? |
| 92 | + |
| 93 | +`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. |
| 94 | + |
| 95 | +### 3. When you use `sort()` does it permanently change the list? |
| 96 | + |
| 97 | +Yes, `sort()` permanently changes the order of elements in the list. The original order is lost unless you make a copy before sorting. |
| 98 | + |
| 99 | +### 4. How to sort a list without using the `sort()` function? |
| 100 | + |
| 101 | +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. |
| 102 | + |
| 103 | +### 5. What is the difference between the `sort()` method and the `sorted()` function? |
| 104 | + |
| 105 | +- `sort()` is a method available only to lists, sorts in place, and returns `None`. |
| 106 | +- `sorted()` is a built-in function that works with any iterable, returns a new sorted list, and leaves the original data unchanged. |
0 commit comments