Full name
sorted
Library
Built-in
Syntax
sorted(iterable, *, key=None, reverse=False)
Description
The sorted function returns a new ordered list from the elements of the iterable passed as an argument.
Parameters
- iterable: Iterable whose elements we want to sort.
- key: (Optional) specifies the single argument function that will be used to determine the sort order of the elements. The default value is None, which implies that the elements will be compared directly.
- reverse: (Optional) boolean argument that determines whether the order will be increasing or decreasing.
Examples
In this example we apply the function to a list:
print(sorted(["k", "f", "c", "h"]))
Now we repeat the example, but applying the function to a tuple. The result is still a list:
print(sorted(("k", "f", "c", "h")))
If we apply the function to a dictionary, it returns the ordered list of keys:
d = dict([(3, "tres"), (1, "uno"), (2, "dos")])
print(sorted(d))