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
Dictionary use cases
๐ Scenario: You are managing a small bookstore's inventory. You want to keep track of book titles and their quantities using a dictionary.
๐ฏ Goal: Build a Python program that creates a dictionary of books and quantities, sets a minimum stock threshold, finds books below that threshold, and prints them.
๐ What You'll Learn
Create a dictionary with exact book titles and quantities
Create a variable for minimum stock threshold
Use a dictionary comprehension to find books with stock below the threshold
Print the resulting dictionary of low stock books
๐ก Why This Matters
๐ Real World
Managing inventory in stores or warehouses often uses dictionaries to track items and quantities.
๐ผ Career
Understanding dictionary operations is essential for data handling, filtering, and reporting in many programming jobs.
Progress0 / 4 steps
1
Create the book inventory dictionary
Create a dictionary called inventory with these exact entries: 'Python Basics': 12, 'Data Science 101': 5, 'Machine Learning': 3, 'Deep Learning': 7, 'AI for Beginners': 2
Python
Hint
Use curly braces {} to create a dictionary with keys as book titles and values as quantities.
2
Set the minimum stock threshold
Create a variable called min_stock and set it to 5 to represent the minimum acceptable stock level.
Python
Hint
Just assign the number 5 to a variable named min_stock.
3
Find books with stock below the threshold
Use a dictionary comprehension to create a new dictionary called low_stock_books that contains only the books from inventory with quantities less than min_stock. Use for book, qty in inventory.items() in your comprehension.
Python
Hint
Use {key: value for key, value in dict.items() if condition} format for dictionary comprehension.
4
Print the low stock books
Write a print statement to display the low_stock_books dictionary.
Python
Hint
Use print(low_stock_books) to show the dictionary.
Practice
(1/5)
1. What is the main purpose of a dictionary in Python?
easy
A. To store data as key-value pairs for quick access
B. To store data in a fixed order like a list
C. To perform mathematical calculations
D. To create a sequence of numbers
Solution
Step 1: Understand dictionary structure
Dictionaries store data in pairs where each key maps to a value.
Step 2: Identify dictionary use case
This structure allows fast lookup by key, unlike lists which use indexes.
Final Answer:
To store data as key-value pairs for quick access -> Option A
Quick Check:
Dictionaries = key-value pairs [OK]
Hint: Remember: dictionaries use keys to find values fast [OK]
Common Mistakes:
Confusing dictionaries with lists or sets
Thinking dictionaries keep order like lists
Assuming dictionaries store only numbers
2. Which of the following is the correct way to add a new key-value pair to a dictionary my_dict?
easy
A. my_dict.append('key', 'value')
B. my_dict.add('key', 'value')
C. my_dict['key'] = 'value'
D. my_dict.insert('key', 'value')
Solution
Step 1: Recall dictionary syntax for adding items
To add or update a key-value pair, use square brackets with the key and assign a value.
Step 2: Check each option
Only my_dict['key'] = 'value' correctly adds or updates the dictionary.
Final Answer:
my_dict['key'] = 'value' -> Option C
Quick Check:
Use square brackets to add/update dict items [OK]
Hint: Use square brackets and assignment to add dict items [OK]
Common Mistakes:
Using list methods like append or insert on dictionaries
When keys repeat, the last value overwrites earlier ones.
Step 2: Check each option
scores = {name: score for name, score in students} uses comprehension that keeps the last score for 'Alice' (95). scores = dict(students)
scores['Alice'] = 85 wrongly resets 'Alice' score. scores = {}
for name, score in students:
if name not in scores:
scores[name] = score keeps only first score. scores = {name: max(score) for name, score in students} is invalid syntax.
Final Answer:
scores = {name: score for name, score in students} -> Option B
Quick Check:
Dict comprehension overwrites duplicate keys [OK]
Hint: Dict comprehension keeps last value for duplicate keys [OK]