0
0
Pythonprogramming~15 mins

Removing dictionary entries in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Removing dictionary entries
๐Ÿ“– Scenario: You are managing a small store's inventory. Some products are out of stock and need to be removed from the inventory list.
๐ŸŽฏ Goal: Build a program that removes products with zero stock from the inventory dictionary.
๐Ÿ“‹ What You'll Learn
Create a dictionary called inventory with product names as keys and stock counts as values.
Create a list called out_of_stock to hold products with zero stock.
Use a for loop to find products with zero stock and add them to out_of_stock.
Remove all products in out_of_stock from inventory.
Print the updated inventory dictionary.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Managing inventory is common in stores and warehouses to keep track of available products and remove items that are not currently sellable.
๐Ÿ’ผ Career
Knowing how to manipulate dictionaries and remove entries based on conditions is useful for data cleaning and management tasks in many programming jobs.
Progress0 / 4 steps
1
Create the inventory dictionary
Create a dictionary called inventory with these exact entries: 'apples': 10, 'bananas': 0, 'oranges': 5, 'pears': 0, 'grapes': 8.
Python
Need a hint?

Use curly braces {} to create the dictionary with the exact keys and values.

2
Create the out_of_stock list
Create an empty list called out_of_stock to store products with zero stock.
Python
Need a hint?

Use square brackets [] to create an empty list.

3
Find and collect out-of-stock products
Use a for loop with variables product and stock to iterate over inventory.items(). Inside the loop, if stock is 0, add product to the out_of_stock list.
Python
Need a hint?

Use inventory.items() to get product and stock pairs. Use append() to add to the list.

4
Remove out-of-stock products and print inventory
Use a for loop to iterate over out_of_stock and remove each product from inventory using del. Then print the updated inventory dictionary.
Python
Need a hint?

Use del inventory[product] to remove a key from the dictionary.