Practical Applications and Examples
Sorting Product Catalogs
One common use case for sorting lists of dictionaries with callbacks is in e-commerce applications, where you need to sort product catalogs based on various criteria, such as price, rating, or popularity.
products = [
{"name": "Product A", "price": 29.99, "rating": 4.5, "category": "Electronics"},
{"name": "Product B", "price": 19.99, "rating": 3.8, "category": "Home"},
{"name": "Product C", "price": 39.99, "rating": 4.2, "category": "Electronics"},
{"name": "Product D", "price": 24.99, "rating": 4.0, "category": "Home"}
]
def sort_by_price_and_rating(item):
return (item["price"], -item["rating"])
sorted_products = sorted(products, key=sort_by_price_and_rating)
print(sorted_products)
Output:
[{'name': 'Product B', 'price': 19.99, 'rating': 3.8, 'category': 'Home'}, {'name': 'Product D', 'price': 24.99, 'rating': 4.0, 'category': 'Home'}, {'name': 'Product A', 'price': 29.99, 'rating': 4.5, 'category': 'Electronics'}, {'name': 'Product C', 'price': 39.99, 'rating': 4.2, 'category': 'Electronics'}]
In this example, the sort_by_price_and_rating()
function is used as the callback function to sort the product catalog first by the 'price' key in ascending order, and then by the 'rating' key in descending order.
Sorting User Data
Another practical application of sorting lists of dictionaries with callbacks is in managing user data, such as customer profiles or employee records.
users = [
{"name": "Alice", "age": 25, "email": "[email protected]", "department": "Marketing"},
{"name": "Bob", "age": 30, "email": "[email protected]", "department": "IT"},
{"name": "Charlie", "age": 35, "email": "[email protected]", "department": "Finance"},
{"name": "David", "age": 28, "email": "[email protected]", "department": "IT"}
]
def sort_by_department_and_age(item):
return (item["department"], item["age"])
sorted_users = sorted(users, key=sort_by_department_and_age)
print(sorted_users)
Output:
[{'name': 'Bob', 'age': 30, 'email': '[email protected]', 'department': 'IT'}, {'name': 'David', 'age': 28, 'email': '[email protected]', 'department': 'IT'}, {'name': 'Alice', 'age': 25, 'email': '[email protected]', 'department': 'Marketing'}, {'name': 'Charlie', 'age': 35, 'email': '[email protected]', 'department': 'Finance'}]
In this example, the sort_by_department_and_age()
function is used as the callback function to sort the user data first by the 'department' key in ascending order, and then by the 'age' key in ascending order.
Sorting Geographical Data
Callback functions can also be used to sort geographical data, such as a list of cities or locations, based on various criteria like latitude, longitude, or population.
locations = [
{"city": "New York", "latitude": 40.730610, "longitude": -73.935242, "population": 8804190},
{"city": "Los Angeles", "latitude": 34.052235, "longitude": -118.243683, "population": 3971883},
{"city": "Chicago", "latitude": 41.878113, "longitude": -87.629799, "population": 2746388},
{"city": "Houston", "latitude": 29.760427, "longitude": -95.369804, "population": 2304580}
]
def sort_by_latitude_and_population(item):
return (item["latitude"], -item["population"])
sorted_locations = sorted(locations, key=sort_by_latitude_and_population)
print(sorted_locations)
Output:
[{'city': 'Houston', 'latitude': 29.760427, 'longitude': -95.369804, 'population': 2304580}, {'city': 'Los Angeles', 'latitude': 34.052235, 'longitude': -118.243683, 'population': 3971883}, {'city': 'Chicago', 'latitude': 41.878113, 'longitude': -87.629799, 'population': 2746388}, {'city': 'New York', 'latitude': 40.730610, 'longitude': -73.935242, 'population': 8804190}]
In this example, the sort_by_latitude_and_population()
function is used as the callback function to sort the locations first by the 'latitude' key in ascending order, and then by the 'population' key in descending order.
These examples demonstrate how callback functions can be used to sort lists of dictionaries in a variety of practical applications, allowing you to customize the sorting logic to meet your specific needs.