Sorting a List of Dictionaries Using Lambda with sorted()
๐ Scenario: You work in a small bookstore. You have a list of books, each with a title and a price. You want to organize the books by their price from lowest to highest so customers can easily see the cheapest books first.
๐ฏ Goal: Build a Python program that sorts a list of dictionaries representing books by their price using sorted() with a lambda function.
๐ What You'll Learn
Create a list called
books with dictionaries containing 'title' and 'price' keysCreate a variable called
sorted_books that sorts books by the 'price' key using sorted() and a lambdaPrint the
sorted_books list๐ก Why This Matters
๐ Real World
Sorting lists of dictionaries is common when working with data like products, books, or records where you want to order items by a specific detail.
๐ผ Career
Many jobs require sorting data efficiently. Using lambda with sorted() is a quick way to organize data without writing complex code.
Progress0 / 4 steps