Dictionary Comprehension with Condition
๐ Scenario: You are managing a small bookstore's inventory. You have a list of books with their prices. You want to create a new dictionary that only includes books priced below a certain amount to plan a discount sale.
๐ฏ Goal: Build a dictionary comprehension with a condition to filter books priced below a set threshold.
๐ What You'll Learn
Create a dictionary called
books with given book titles and prices.Create a variable called
max_price to set the price limit.Use a dictionary comprehension with a condition to create a new dictionary called
discount_books with books priced less than max_price.Print the
discount_books dictionary.๐ก Why This Matters
๐ Real World
Filtering products or items based on price or other conditions is common in online stores and inventory management.
๐ผ Career
Knowing how to filter dictionaries efficiently helps in data processing, reporting, and building features like price filters in apps.
Progress0 / 4 steps