Sorting Data Efficiently
Part of the Logic & Flow section of Coddy's Python journey — lesson 48 of 78.
Sorting is a fundamental operation in computer science, and Python offers powerful built-in tools to sort data efficiently. The primary function for sorting is sorted(), which can be used to sort various types of data, including numbers, strings, and more complex objects.
Basic Sorting:
The sorted() function takes an iterable (e.g., a list, tuple, or set) as its argument and returns a new list containing the sorted elements. By default, it sorts in ascending order.
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
# Output: [1, 1, 2, 3, 4, 5, 6, 9]In this example, sorted() sorts the numbers list in ascending order.
Reverse Sorting:
To sort in descending order, you can use the reverse parameter and set it to True.
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_numbers_desc = sorted(numbers, reverse=True)
print(sorted_numbers_desc)
# Output: [9, 6, 5, 4, 3, 2, 1, 1]Here, sorted() sorts the numbers list in descending order.
Sorting Strings:
The sorted() function can also sort strings based on their lexicographical order (i.e., the order they would appear in a dictionary).
words = ["apple", "banana", "cherry"]
sorted_words = sorted(words)
print(sorted_words)
# Output: ['apple', 'banana', 'cherry']In this example, sorted() sorts the words list in alphabetical order.
Custom Sorting with Key Function:
For more complex sorting needs, you can use the key parameter to specify a function that determines the sorting order. The key function is applied to each element before sorting, and the returned values are used for comparison.
words = ["banana", "kiwi", "fig"]
sorted_words_by_length = sorted(words, key=len)
print(sorted_words_by_length)
# Output: ['fig', 'kiwi', 'banana']In this case, sorted() sorts the words list based on the length of each word, using the len() function as the key.
Challenge
EasyWrite a program that performs the following sorting tasks using the sorted() function:
- Sort a list of numbers in ascending order.
- Sort the same list of numbers in descending order.
- Sort a list of strings in alphabetical order.
- Sort the same list of strings based on their length.
Cheat sheet
The sorted() function sorts data and returns a new list with sorted elements.
Basic sorting (ascending order):
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_numbers = sorted(numbers)
# Output: [1, 1, 2, 3, 4, 5, 6, 9]Reverse sorting (descending order):
sorted_numbers_desc = sorted(numbers, reverse=True)
# Output: [9, 6, 5, 4, 3, 2, 1, 1]Sorting strings alphabetically:
words = ["apple", "banana", "cherry"]
sorted_words = sorted(words)
# Output: ['apple', 'banana', 'cherry']Custom sorting with key function:
sorted_words_by_length = sorted(words, key=len)
# Sorts by string lengthTry it yourself
# Starter inputs
numbers = [5, 3, 8, 1, 2]
words = ["elephant", "cat", "dolphin", "bee"]
# Task 1: Sort numbers in ascending order
# Task 2: Sort numbers in descending order
# Task 3: Sort words alphabetically
# Task 4: Sort words by length
# Replace 'pass' with your code for each task
ascending_numbers = None
descending_numbers = None
alphabetical_words = None
length_sorted_words = None
# Print the results
print("Ascending:", ascending_numbers)
print("Descending:", descending_numbers)
print("Alphabetical:", alphabetical_words)
print("By Length:", length_sorted_words)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter