Python sorted() Function
The sorted() function returns a sorted list of the specified
iterable object.
You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.
Syntax
sorted(iterable, key=key, reverse=reverse)
Parameters
| Parameter | Description |
|---|---|
iterable |
Required. The sequence to sort (list, dictionary, tuple, etc.) |
key |
Optional. A function to execute to decide the order. Default is None |
reverse |
Optional. A Boolean. False will sort ascending, True will sort descending. Default is False |
Return Value
A new list containing the sorted elements.
Example
Using the sorted() function:
Output
Click Run to execute your code
Enjoying these tutorials?