Sorting is an important operation that helps you understand and manage your data more effectively.
In Python, we have a few methods which can be used to sort data in lists, dictionaries, or other data structures. This tutorial is designed to help you learn how to sort data in Python using various techniques and best practices.
Step 1: Sorting Lists using the sort() Method
The sort()
method is a built-in function that can be used to sort the elements of a list in ascending or descending order.
Here’s an example:
1 2 3 |
numbers = [7, 2, 5, 8, 6] numbers.sort() print(numbers) |
1 2 3 |
numbers = [7, 2, 5, 8, 6] numbers.sort() print(numbers) |
Output:
[2, 5, 6, 7, 8]
If you want to sort the list in descending order, use the reverse=True
argument:
1 2 |
numbers.sort(reverse=True) print(numbers) |
Step 2: Sorting Lists using the sorted() Function
If you don’t want to modify the original list but want to create a new sorted list, use the built-in sorted()
function.
Example:
1 2 3 4 |
original_list = [7, 2, 5, 8, 6] sorted_list = sorted(original_list) print(original_list) print(sorted_list) |
1 2 3 4 |
original_list = [7, 2, 5, 8, 6] sorted_list = sorted(original_list) print(original_list) print(sorted_list) |
Output:
[7, 2, 5, 8, 6] [2, 5, 6, 7, 8]
To sort the list in descending order, use the reverse=True
argument:
1 2 |
sorted_list = sorted(original_list, reverse=True) print(sorted_list) |
Step 3: Sorting Dictionaries using the sorted() Function
To sort a dictionary based on its keys or values, you can use the sorted()
function along with dictionary.items()
and key
argument.
Here’s an example of sorting a dictionary by its keys:
1 2 3 |
character_frequencies = {'e': 5, 'a': 3, 'r': 2, 't': 1} sorted_dict = sorted(character_frequencies.items()) print(sorted_dict) |
1 2 3 |
character_frequencies = {'e': 5, 'a': 3, 'r': 2, 't': 1} sorted_dict = sorted(character_frequencies.items()) print(sorted_dict) |
Output:
[('a', 3), ('e', 5), ('r', 2), ('t', 1)]
To sort a dictionary by its values in ascending order:
1 2 |
sorted_dict = sorted(character_frequencies.items(), key=lambda item: item[1]) print(sorted_dict) |
[('t', 1), ('r', 2), ('a', 3), ('e', 5)]
To sort a dictionary by its values in descending order:
1 2 |
sorted_dict = sorted(character_frequencies.items(), key=lambda item: item[1], reverse=True) print(sorted_dict) |
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
numbers = [7, 2, 5, 8, 6] numbers.sort() print(numbers) original_list = [7, 2, 5, 8, 6] sorted_list = sorted(original_list) print(original_list) print(sorted_list) character_frequencies = {'e': 5, 'a': 3, 'r': 2, 't': 1} sorted_dict = sorted(character_frequencies.items()) print(sorted_dict) sorted_dict = sorted(character_frequencies.items(), key=lambda item: item[1]) print(sorted_dict) sorted_dict = sorted(character_frequencies.items(), key=lambda item: item[1], reverse=True) print(sorted_dict) |
Conclusion
This tutorial explains how to sort data in Python using the sort()
method, sorted()
function, and sorting dictionaries by keys or values. These techniques will help you manage and work with your data more efficiently.
Remember to choose the most suitable method based on your application and if you need to maintain the original order or create a new sorted data structure.