In this tutorial, you will learn everything about the Python sorted() function along with different-different examples. The sorted() function is used to sort any iterable in ascending and descending order.
In previous tutorials, we have seen lots of Python built-in functions along with examples.
Headings of Contents
Python sorted() function Introduction
Python sorted() function is a built-in function that is used to sort the specified iterable object. sorted() function sort any sequence (list, tuple, set) and always return a list in a sorted manner, without modifying the original sequence.
You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.
Syntax
The syntax of sorted function in Python is:-
sorted(iterable, key = key, reverse = False)
sorted() Parameters
sorted function in Python takes three parameters.
- iterable:- Required. The sequence to sort, list, dictionary, tuple, etc.
- key:- Optional. A Function to execute to decide the order. The default is None.
- reverse:- Optional. A Boolean. False will sort ascending, True will sort descending. The default is False.
Note:- You cannot sort a list that contains both string values and numeric values.
sorted() Return Value
It always returns a list in a sorted version.
Python sorted function examples
Here we have taken various examples to understand the Python sorted function.
Example:- Sorting list in Python
# Sort the list
a = [1, 3, 2, 4, 5, 6, 8, 9, 10]
b = sorted(a)
# Sort the list in ascending order
print('Sorted list is:- ', b)
# Sort the list in descending order
c = sorted(a, reverse = True)
print('Reverse sort: ', c)
# Original list
print('Original list:- ', a)
Output
Sorted list is:- [1, 2, 3, 4, 5, 6, 8, 9, 10]
Reverse sort: [10, 9, 8, 6, 5, 4, 3, 2, 1]
Original list:- [1, 3, 2, 4, 5, 6, 8, 9, 10]
Example: Sorting dictionary in Python
myDict = {"first_name": "Vishvajit", "last_name": "Rao", "age": 22, "country": "India"}
# sorted the dictionary
a = sorted(myDict.items())
# convert into dict
print(dict(a))
Output
{'age': 22, 'country': 'India', 'first_name': 'Vishvajit', 'last_name': 'Rao'}
Sorting in Ascending order
When you want to sort in ascending order then you don’t need to mention the reverse parameter in the sorted function.
Example: Sorting list in Python
a = ["h", "b", "a", "c", "f", "d", "e", "g"]
x = sorted(a)
print(x)
Output
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Sorting in Descending order
To sort any iterable in descending order, you have to pass reverse = True in the sorted() function.
Example: Sorting tuples in Python
a = ("h", "b", "a", "c", "f", "d", "e", "g")
x = sorted(a, reverse = True)
print(x)
Output
['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
Using key parameter in sorted() function
Sometimes, you want to implement your own function to perform sorting. To use a custom function, you have to pass a function to the key parameter in the sorted() function.
For example, I have a Python list having some names of the programming languages. Now I want to sort the list of items in ascending order on the basis of item length. I will sort in both ascending and descending order.
Example: Sorting list in ascending order using the key parameter
programming_languages = ['Python', "Java", "PHP", "C", "C++", "Scala", "Dot Net"]
result = sorted(programming_languages, key=len)
print("Sorted List:- ", result)
print("Original List:- ", programming_languages)
Output
Sorted List:- ['C', 'PHP', 'C++', 'Java', 'Scala', 'Python', 'Dot Net']
Original List:- ['Python', 'Java', 'PHP', 'C', 'C++', 'Scala', 'Dot Net']
As you can see in the above output, the list programming_languages has been sorted in ascending order according to the item present in the list.
Example: Sorting list in descending order using the key parameter
programming_languages = ['Python', "Java", "PHP", "C", "C++", "Scala", "Dot Net"]
result = sorted(programming_languages, key=len, reverse=True)
print("Sorted List:- ", result)
print("Original List:- ", programming_languages)
Output
Sorted List:- ['Dot Net', 'Python', 'Scala', 'Java', 'PHP', 'C++', 'C']
Original List:- ['Python', 'Java', 'PHP', 'C', 'C++', 'Scala', 'Dot Net']
As you can see in the above output, the list programming_languages has been sorted in descending order according to the item present in the list.
Conclusion
In this article, you have learned all about Python’s sorted() function along with examples. Python sorted function is the best function to sort any specified iterable in ascending order or descending order.
If you like this article, please share and keep visiting for further Python built-in functions tutorials.
Python built-in functions
For more information:- Click Here
Thanks for your valuable time…..🙏🙏❤️❤️