list.sort() Sort in place
Last updated: 2026-09-22
list.sort() Sort in place
list.sort(*, key=None, reverse=False) sorts in place; returns None.
| Category | List Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
list.sort(*, key=None, reverse=False)
⚙️ Parameters
| key Optional | Function to extract a sort key from each element; default is direct comparison.(Default:None) |
|---|---|
| reverse Optional | Whether to sort in descending order; default False (ascending).(Default:False) |
Returns:None (sorts in place; use sorted() for a new list).
Mutates the original:Yes (in place)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
[1, 2, 3]
[3, 2, 1]
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does sort() return?
AIt returns None! It sorts in place, so never write lst = lst.sort() (that would turn the list into None).
QHow do I keep the original list?
AUse sorted(lst), which returns a new sorted list while leaving the original unchanged.
QHow do I sort by string length?
AUse the key parameter: lst.sort(key=len); add reverse=True for descending order.
QIs the sort stable?
AYes; elements with equal keys keep their original relative order. In-place sorting uses less memory than sorted().