Complete Python Series · Part 24 of 29
Assignment 11-catalog-sorting
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Course Catalog Sorting System
Learn to build reusable sorting functions using lambda expressions, higher-order functions, type hints and the built-in sorted() function.
Problem statement
NextGen CTO maintains a catalog of courses, each with a name, price and enrollment count. Students want to browse affordable courses, while administrators want to analyze enrollment counts. Build one reusable sorting function that accepts different sorting criteria as functions. All prices and enrollment counts are illustrative.
Task requirements
- Use the provided list of course dictionaries.
- Implement
sort_courses(items: list, key_func) -> listusingsorted(). - Define a lambda function that extracts the course price.
- Define another lambda function that extracts the enrollment count.
- Call the reusable sorting function with each lambda and print both results in ascending order.
- Preserve the original list.
Constraints
- Use
sorted()with itskeyargument. - Use lambda expressions for the two sorting criteria.
- Do not use separate sorting implementations or modify the original list.
- Assume all records contain the required keys and valid numeric values.
Good luck trying the exercise!
Starter code
courses = [
{"name": "Python", "price": 999, "enrollments": 500},
{"name": "DSA", "price": 1499, "enrollments": 850},
{"name": "System Design", "price": 1999, "enrollments": 200},
]
def sort_courses(items: list, key_func) -> list:
# TODO: Return a new sorted list using key_func
pass
# TODO: Define a lambda to extract the price
# by_price = ...
# TODO: Define a lambda to extract the enrollment count
# by_enrollments = ...
# TODO: Call the function with both sorting criteria and print the results
# print("By Price:", sort_courses(courses, by_price))
# print("By Enrollments:", sort_courses(courses, by_enrollments))
Hints
sorted(items, key=key_func)creates a new sorted list.- A lambda is a small anonymous function with syntax
lambda parameter: expression. - Access dictionary values using
course["price"]orcourse["enrollments"]. - Pass the function itself as an argument rather than calling it.
Solution: NextGen CTO Course Catalog Sorting System
The solution separates the sorting operation from the choice of sorting key. The reusable function accepts a callable that extracts a comparable value from each course dictionary.
courses = [
{"name": "Python", "price": 999, "enrollments": 500},
{"name": "DSA", "price": 1499, "enrollments": 850},
{"name": "System Design", "price": 1999, "enrollments": 200},
]
def sort_courses(items: list, key_func) -> list:
return sorted(items, key=key_func)
by_price = lambda course: course["price"]
by_enrollments = lambda course: course["enrollments"]
print("By Price:", sort_courses(courses, by_price))
print("By Enrollments:", sort_courses(courses, by_enrollments))
Expected output
By Price: [{'name': 'Python', 'price': 999, 'enrollments': 500}, {'name': 'DSA', 'price': 1499, 'enrollments': 850}, {'name': 'System Design', 'price': 1999, 'enrollments': 200}]
By Enrollments: [{'name': 'System Design', 'price': 1999, 'enrollments': 200}, {'name': 'Python', 'price': 999, 'enrollments': 500}, {'name': 'DSA', 'price': 1499, 'enrollments': 850}]
Solution explanation
- Lines 1–5: Store course records as a list of dictionaries.
- Lines 8–9: Define a higher-order function that accepts a list and a key function. The built-in
sorted()applies the key function to each item and returns a new list in ascending order. - Line 12: Define a lambda that returns the price of a course.
- Line 13: Define a lambda that returns the enrollment count of a course.
- Lines 15–16: Reuse the same sorting function with different key functions and print the results.
For learning purposes, the lambdas are assigned names so they can be passed around explicitly. In production Python, PEP 8 generally prefers def over assigning a lambda directly to a name.