
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Sorted() function in Python
In this tutorial, we are going to learn about the sorted() function in Python.
The function sorted() is used to sort an iterable in ascending or descending order. We can even sort the list of dictionaries based on different keys and values. Let's get most out of the sorted() function.
The sorted() function is not an in-place algorithm like sort method.
Default sorted()
The function sorted() will sort an iterable in ascending order by default. Let's see an example.
Example
# initializing a list numbers = [4, 3, 5, 1, 2] # sorting the numbers sorted_numbers = sorted(numbers) # printing the sorted_numbers print(sorted_numbers)
Output
If you run the above code, then you will get the following result.
[1, 2, 3, 4, 5]
reverse sorted()
We can set a parameter reverse as True to sort the iterable in descending order. Let's see an example.
Example
# initializing a list numbers = [4, 3, 5, 1, 2] # sorting the numbers sorted_numbers = sorted(numbers, reverse=True) # printing the sorted_numbers print(sorted_numbers)
Output
If you run the above code, then you will get the following result.
[5, 4, 3, 2, 1]
key parameter with sorted()
The function sorted() will take another optional parameter called key. The parameter key is to tell the sorted() on which value it has to sort the list.
Let's say we have a list of dictionaries. We have to sort the list of dictionaries based on a certain value. In this case, we pass key as a parameter with a function that returns a specific value on which we have to sort the list of dictionaries.
Example
# initializing a list numbers = [{'a': 5}, {'b': 1, 'a': 1}, {'c': 3, 'a': 3}, {'d': 4, 'a': 4}, {'e' 'a': 2}] # sorting the list of dict based on values sorted_dictionaries = sorted(numbers, key= lambda dictionary: dictionary['a']) # printing the numbers print(sorted_dictionaries)
Output
If you run the above code, then you will get the following result.
[{'b': 1, 'a': 1}, {'e': 2, 'a': 2}, {'c': 3, 'a': 3}, {'d': 4, 'a': 4}, {'a':
Conclusion
If you have any doubts in the tutorial, mention them in the comment section.
- Related Articles
- Sorted(): My favorite function in Python
- Merge Sorted Array in Python
- Merge Two Sorted Lists in Python
- Merge k Sorted Lists in Python
- Combining two sorted lists in Python
- Python – Filter Sorted Rows
- Python – Extract Sorted Strings
- Search in Rotated Sorted Array in Python
- Program to merge two sorted list to form larger sorted list in Python
- Remove Duplicates from Sorted Array in Python
- Delete Columns to Make Sorted in Python
- Search in Rotated Sorted Array II in Python
- How to generate a sorted list in Python?
- Merge two sorted arrays in Python using heapq?
- Python - Inserting item in sorted list maintaining order
