sorted() Sorted new list

Last updated: 2026-09-22

sorted() Sorted new list

sorted(iterable, *, key=None, reverse=False) returns a new sorted list; original unchanged; key and reverse options available.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

sorted(iterable, *, key=None, reverse=False)

⚙️ Parameters

iterable RequiredThe iterable to sort.
key OptionalKey function applied to each element.(Default:None)
reverse OptionalSort descending when True.(Default:False)

Returns:A new sorted list; the original is unchanged.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

[1, 2, 3]
['apple', 'banana']
More case studies coming soon — check back later.

❓ FAQ

QWhat is the difference between sorted() and list.sort()?
Asorted() returns a new list, leaves the original unchanged, and works on any iterable; lst.sort() sorts in place, returns None, and only applies to lists.
QAfter sorting, is the original list changed?
ANo. sorted([3, 1, 2]) returns a new list [1, 2, 3] while the original stays [3, 1, 2]; use lst.sort() to sort in place.
QWhat does sorted(lst, key=len, reverse=True) mean?
AIt sorts by string length in descending order: key sets the comparison basis (computed once per element) and reverse=True means descending.
QWhy does sorted([1, 'a']) raise a TypeError?
Aint and str have no defined ordering, so mixed types cannot be sorted; ensure the element types are consistent or provide a key before sorting.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏