Python sorted()
The sorted() is a built-in python function that returns a sorted list of the given iterable object.
Python sorted()
The syntax of sorted() is:
1
2
sorted(iterable, key, reverse)
sorted() Parameters
The sorted() function takes three parameters as argument:
- iterable - an iterable object like string, list,set, etc.
- key (Optional) - A Function to execute to decide the order. Default is None.
- reverse (Optional) -
Let see an example of the sorted() function.
Example 1: How to use sorted() function in python?
1
2
3
4
a = (2, 5, 3)
x = sorted(a)
print(x)
Output:
1
2
[2, 3, 5]
Example 2: sorted() function with python string.
1
2
3
4
a = ("h", "b", "a", "c", "f", "d", "e", "g")
x = sorted(a)
print(x)
Output:
1
2
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
This post is licensed under CC BY 4.0 by the author.