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
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
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
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
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
Hint
Use del inventory[product] to remove a key from the dictionary.
Practice
(1/5)
1. What does the pop(key) method do when used on a Python dictionary?
easy
A. Removes the key and returns its value
B. Removes the key without returning its value
C. Returns the value without removing the key
D. Deletes the entire dictionary
Solution
Step 1: Understand the pop method
The pop(key) method removes the specified key from the dictionary and returns its value.
Step 2: Compare with other methods
Unlike del dict[key], pop returns the removed value.
Final Answer:
Removes the key and returns its value -> Option A
Quick Check:
pop(key) = remove key + return value [OK]
Hint: pop returns value and removes key from dict [OK]
Common Mistakes:
Thinking pop only returns value without removal
Confusing pop with del which doesn't return value
Assuming pop deletes entire dictionary
2. Which of the following is the correct syntax to remove a key 'age' from dictionary person without returning its value?
easy
A. person.delete('age')
B. person.pop('age')
C. person.remove('age')
D. del person['age']
Solution
Step 1: Identify syntax for deletion without return
To remove a key without returning its value, use del dict[key].
Step 2: Check options
del person['age'] uses del person['age'], which is correct syntax. Options C and D are invalid methods.
Final Answer:
del person['age'] -> Option D
Quick Check:
del dict[key] removes key without return [OK]
Hint: Use del dict[key] to remove key without return [OK]
Common Mistakes:
Using pop when no return is needed
Using non-existent methods like remove or delete
Wrong syntax like person.delete('age')
3. What is the output of this code?
data = {'a': 1, 'b': 2, 'c': 3}
value = data.pop('b')
print(value, data)
medium
A. 2 {'a': 1, 'b': 2, 'c': 3}
B. None {'a': 1, 'c': 3}
C. 2 {'a': 1, 'c': 3}
D. KeyError
Solution
Step 1: Use pop to remove key 'b'
The pop('b') removes key 'b' and returns its value 2.
Step 2: Print returned value and updated dictionary
After removal, dictionary is {'a': 1, 'c': 3}. Printing value and dict shows: 2 {'a': 1, 'c': 3}.
Final Answer:
2 {'a': 1, 'c': 3} -> Option C
Quick Check:
pop returns value and removes key [OK]
Hint: pop returns value and removes key from dict [OK]
Common Mistakes:
Expecting original dict unchanged
Thinking pop returns None
Confusing pop with del which returns nothing
4. What error will this code produce?
info = {'name': 'Alice', 'age': 30}
info.pop('gender')
medium
A. None
B. KeyError
C. Returns 'gender'
D. SyntaxError
Solution
Step 1: Understand pop behavior without default
Calling pop on a missing key without a default raises a KeyError.
Step 2: Check code
Since 'gender' is not in info, info.pop('gender') raises KeyError.
Final Answer:
KeyError -> Option B
Quick Check:
pop missing key without default = KeyError [OK]
Hint: pop missing key without default causes KeyError [OK]
Common Mistakes:
Expecting None instead of error
Thinking pop returns the key name
Confusing with pop(key, default)
5. Given items = {'apple': 5, 'banana': 0, 'cherry': 7}, which code removes all entries with value 0 or less safely without errors?
hard
A. for k in list(items):
if items[k] <= 0:
items.pop(k)
B. for k in items:
if items[k] <= 0:
del items[k]
C. items.pop('banana')
D. del items['banana']
Solution
Step 1: Avoid modifying dict while iterating
Directly deleting keys while iterating causes runtime errors. Using list(items) copies keys safely.
Step 2: Use pop to remove keys with value โค 0
Loop over copied keys, check value, and use pop to remove safely.
Final Answer:
for k in list(items):
if items[k] <= 0:
items.pop(k) -> Option A
Quick Check:
Iterate on list copy + pop keys safely [OK]
Hint: Iterate on list(items) to safely remove keys while looping [OK]
Common Mistakes:
Deleting keys directly while iterating causes error