Python Reference ยท Lists, Tuples and Ranges
Python list.sort() method
Reference for Python list.sort(): sort a list in place, the key and reverse arguments, stability, mixed-type errors and how it differs from sorted().
The list.sort() method sorts the items of a list in place, from smallest to largest by default. It changes the list itself and returns None, so there is no new list to assign.
You control the order with two keyword-only arguments: key, a function that computes the value to compare for each item, and reverse, which flips the result to descending order. The sort is stable, meaning items that compare equal keep their original relative order.
scores = [30, 10, 20] scores.sort() print(scores) # [10, 20, 30]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
list.sort(*, key=None, reverse=False)
| Name | Description |
|---|---|
key |
Optional. A function of one argument. It is called once per item, and the results decide the order. None compares the items themselves. |
reverse |
Optional. If True, the list is sorted in descending order. The default is False. |
Both parameters are keyword-only: write sort(key=len), not sort(len).
Return value: None. The list is sorted in place.
Raises: TypeError if two items cannot be compared with <, such as an int and a str.
Usage notes
- It returns
None. Writingitems = items.sort()destroys your list. Usesorted()when you want a new sorted list and an untouched original. - Stable sort. Items with equal keys keep their original order. That lets you sort in several passes: sort by the secondary key first, then by the primary key.
reverse=Truekeeps stability. Equal items still keep their original order; the result is not simply the ascending result read backward.keyis called once per item, not once per comparison, so an expensive key function is fine. The key values are not stored in the result.- Case-sensitive by default. Uppercase letters sort before lowercase ones, so use
key=str.lower(orstr.casefold) for a case-insensitive order. - Tuples compare element by element, so a key that returns a tuple, such as
(last_name, first_name), sorts by several fields. - Mixed types fail in Python 3. Convert the items or use a key that makes them comparable.
- If a comparison fails midway, the list may be left partly rearranged. During the sort, the list appears empty to other code; do not modify it from inside
key. - Old-style
cmpfunctions are gone. Wrap one withfunctools.cmp_to_keyif you must. - Speed: the algorithm (Timsort) is very fast on data that is already partly ordered.
Related
sorted(),list.reverse(),reversed(),list- Lessons: Lists, List methods, Lambda functions