Home >>Python Built-in Functions >Python sorted() Function
Python sorted() function is used to return a sorted list of the given input iterable object. You can specify ascending or descending order.
Syntax:sorted(iterable, key=key, reverse=reverse)
Parameter | Description |
---|---|
iterable | This is a required parameter. It defines the sequence to sort, list, dictionary, tuple etc. |
key | This is an optional parameter. It defines a function to execute to decide the order. |
reverse | This is an optional parameter. It defines a Boolean value. |
a = ("b", "g", "a", "d", "f", "c", "h", "e")
x = sorted(a)
print(x)
a = ("h", "b", "a", "c", "f", "d", "e", "g")
x = sorted(a, reverse=True)
print(x)