Built-in Functions
sorted()
Make a new list containing items in sorted order.
sorted(iterable, *, key=None, reverse=False)
sorted() returns a new sorted list from the items in any iterable without modifying the original collection.
Parameters
| Name | Description |
|---|---|
| iterable | The items to sort. |
| key | Optional function used to choose a sorting value. |
| reverse | Use True to sort from largest to smallest. |
Returns
A new sorted list.
Try it
Run it and change it
scores = [12, 5, 9] print(sorted(scores))
Worth knowing
- sorted() vs list.sort():
sorted()works on any iterable (strings, tuples, sets, dicts) and returns a new list;list.sort()only works on lists in place.