Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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' keys
Create a variable called sorted_books that sorts books by the 'price' key using sorted() and a lambda
Print 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
1
Create the list of books
Create a list called books with these exact dictionaries: {'title': 'Book A', 'price': 12.99}, {'title': 'Book B', 'price': 8.99}, and {'title': 'Book C', 'price': 15.50}.
Python
Hint
Use square brackets [] to create a list and curly braces {} for each dictionary.
2
Prepare to sort the books
Create a variable called sorted_books and set it to None for now. This will hold the sorted list later.
Python
Hint
Just write sorted_books = None for now.
3
Sort the books by price using lambda with sorted()
Use sorted() with a lambda function to sort the books list by the 'price' key. Assign the result to sorted_books. Use lambda book: book['price'] as the key function.
Python
Hint
Use sorted(books, key=lambda book: book['price']) to sort by price.
4
Print the sorted list of books
Print the sorted_books list to show the books sorted by price.
Python
Hint
Use print(sorted_books) to show the sorted list.
Practice
(1/5)
1. What does the key argument do when used with sorted() and a lambda function in Python?
easy
A. It reverses the order of the sorted list automatically.
B. It changes the original list to a sorted list in place.
C. It tells sorted() how to compare items by extracting a value from each item.
D. It filters out items that do not match the lambda condition.
Solution
Step 1: Understand the role of key in sorted()
The key argument takes a function that extracts a value from each item to use for sorting.
Step 2: Understand how lambda works with key
The lambda function defines this extraction rule inline, telling sorted() what to compare.
Final Answer:
It tells sorted() how to compare items by extracting a value from each item. -> Option C
Quick Check:
key=lambda x: value means compare by value [OK]
Hint: Remember: key=lambda extracts sort value from each item [OK]
Common Mistakes:
Thinking key changes the original list
Confusing key with reverse sorting
Assuming lambda filters items
2. Which of the following is the correct syntax to sort a list of tuples data = [(2, 'b'), (1, 'a'), (3, 'c')] by the first element using sorted() and a lambda?
easy
A. sorted(data, key=lambda x: x[0])
B. sorted(data, lambda x: x[0])
C. sorted(data, key=lambda x: x[1])
D. sorted(data, key=x[0])
Solution
Step 1: Identify the correct use of key argument
The key argument must be named and assigned a function, here a lambda.
Step 2: Check lambda syntax and index
lambda x: x[0] correctly extracts the first element of each tuple for sorting.
Final Answer:
sorted(data, key=lambda x: x[0]) -> Option A
Quick Check:
Correct syntax uses key= and lambda with index [OK]
Hint: Always use key= with lambda for sorting rules [OK]
The lambda extracts the length of each string: apple(5), banana(6), cherry(6).
Step 2: Sort strings by their length
Sorted order by length is: apple(5), banana(6), cherry(6). Since banana and cherry have same length, original order among them is preserved (banana before cherry).
Final Answer:
['apple', 'banana', 'cherry'] -> Option A
Quick Check:
Sort by len(x) = ['apple', 'banana', 'cherry'] [OK]
Hint: Sort by length with key=lambda x: len(x) [OK]