Built-in Functions and Types
sorted()
Consume an iterable and produce a new ordered list. A key callable computes the comparison value for each item once; absent a key, items are compared directly. Equal keys preserve original relative order, including reverse sorting. Incomparable keys can raise TypeError. The input list itself is not rearranged; elements are shared, not deep-copied.
Formal Syntax & Structure
python syntaxpython
sorted(iterable, /, *, key=None, reverse=False)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| iterable | iterable | Items to order; positional only. |
| key | callable or None | Keyword-only one-argument key extractor. |
| reverse | bool | Keyword-only; descending order if true. |
Return Value
list
New stable sorted list.
Working Example
Example Snippetpython
jobs = [("A", 2), ("B", 1), ("C", 2)]print(sorted(jobs, key=lambda job: job[1]))print(sorted(jobs, key=lambda job: job[1], reverse=True))