Python’s sorted()
is a built-in function that returns a sorted list of the elements in an iterable. The iterable can be a list, tuple, dictionary, or any other iterable object. The function takes two optional arguments: key
and reverse
. The key
argument is used to specify a function to be applied to each element of the iterable. The reverse
argument is a boolean value that specifies whether to sort the iterable in ascending or descending order.
How to Use Python Sorted
Sorting a List
Let’s start with a simple example of how to use Python sorted()
to sort a list in ascending order:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
Output:
[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
In this example, we created a list of numbers and used the sorted()
function to sort the list in ascending order. The resulting sorted list is assigned to the sorted_numbers
variable, which is then printed to the console.
Sorting a Tuple
We can also use Python sorted()
to sort a tuple in ascending order:
letters = ('c', 'a', 'b', 'd')
sorted_letters = sorted(letters)
print(sorted_letters)
Output:
['a', 'b', 'c', 'd']
In this example, we created a tuple of letters and used the sorted()
function to sort the tuple in ascending order. The resulting sorted list is assigned to the sorted_letters
variable, which is then printed to the console.
Sorting a Dictionary
When we use Python sorted()
on a dictionary, it returns a list of keys sorted in ascending order by default. Let’s take a look at an example:
fruits = {'apple': 3, 'banana': 2, 'orange': 1, 'pear': 4}
sorted_fruits = sorted(fruits)
print(sorted_fruits)
Output:
['apple', 'banana', 'orange', 'pear']
In this example, we created a dictionary of fruits and their corresponding quantities. We used the sorted()
function to sort the dictionary by its keys in ascending order. The resulting sorted list of keys is assigned to the sorted_fruits
variable, which is then printed to the console.
If we want to sort the dictionary by its values instead of its keys, we can use the key
argument to specify a function that returns the values of the dictionary. Here’s an example:
fruits = {'apple': 3, 'banana': 2, 'orange': 1, 'pear': 4}
sorted_fruits = sorted(fruits, key=lambda x: fruits[x])
print(sorted_fruits)
Output:
['orange', 'banana', 'apple', 'pear']
In this example, we used the sorted()
function with the key
argument to sort the dictionary by its values in ascending order. We passed a lambda function to the key
argument that returns the value of each key in the dictionary. The resulting sorted list of keys is assigned to the sorted_fruits
variable, which is then printed to the console.
Sorting in Descending Order
To sort an iterable in descending order, we can use the reverse
argument of Python sorted()
. Let’s take a look at an example:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)
Output:
[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
In this example, we used the sorted()
function with the reverse
argument set to True
to sort the list in descending order. The resulting sorted list is assigned to the sorted_numbers
variable, which is then printed to the console.
Sorting by Length
We can also use Python sorted()
to sort an iterable by the length of its elements. Let’s take a look at an example:
words = ['banana', 'apple', 'pear', 'orange', 'kiwi']
sorted_words = sorted(words, key=len)
print(sorted_words)
Output:
['kiwi', 'pear', 'apple', 'banana', 'orange']
In this example, we used the sorted()
function with the key
argument set to len
to sort the list by the length of its elements in ascending order. The resulting sorted list is assigned to the sorted_words
variable, which is then printed to the console.
Conclusion
In this article, we explored how to use Python sorted()
function to sort an iterable in ascending or descending order. We provided code examples to illustrate its usage with lists, tuples, and dictionaries, and explained how to sort an iterable by its values or length. Python sorted()
is a powerful function that can be used in various applications, and we hope this guide has helped you understand how to use it effectively.