Skip to content

Commit 97638c5

Browse files
authored
[Edit] Python: .sort() (#7448)
* [Edit] Python: .sort() * updated faqs based on PAA ---------
1 parent 8d4ec14 commit 97638c5

File tree

1 file changed

+76
-19
lines changed
  • content/python/concepts/lists/terms/sort

1 file changed

+76
-19
lines changed
Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,106 @@
11
---
22
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.'
44
Subjects:
5-
- 'Data Science'
65
- 'Computer Science'
6+
- 'Data Science'
77
Tags:
8+
- 'Elements'
89
- 'Lists'
910
- 'Methods'
11+
- 'Python'
1012
CatalogContent:
1113
- 'learn-python-3'
1214
- 'paths/data-science'
13-
- 'paths/computer-science'
1415
---
1516

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:**
1731

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:
1937

2038
```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)
2246
```
2347

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:
2549

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()`
2756

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:
2958

30-
```python
31-
name = ['c', 'h', 'l', 'o', 'e']
59+
```py
60+
fruits = ["apple", "banana", "cherry", "date"]
3261

33-
name.sort()
62+
fruits.sort()
3463

35-
print(name)
36-
# Output: ['c', 'e', 'h', 'l', 'o']
64+
print(fruits)
3765
```
3866

39-
## Codebyte Example
67+
Here is the output:
4068

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:
4276

4377
```codebyte/python
44-
exampleList = [4, 2, 1, 3]
78+
words = ["Python", "is", "amazing", "AI"]
4579
46-
exampleList.sort()
80+
words.sort(key=len)
4781
48-
print(exampleList)
82+
print(words)
4983
```
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

Comments
 (0)