QuestionJuly 16, 2025

Question Goal: Learn to delete dictionary entries in bulk. Assignment: The dictionary stock keeps track of the available items in a store and their price. The list sold0ut keeps track of which items are sold out at the end of the day. Write a code snippet that removes , the items in stock that were sold out. Note: all the items in soldout are a key of stock.

Question Goal: Learn to delete dictionary entries in bulk. Assignment: The dictionary stock keeps track of the available items in a store and their price. The list sold0ut keeps track of which items are sold out at the end of the day. Write a code snippet that removes , the items in stock that were sold out. Note: all the items in soldout are a key of stock.
Question
Goal: Learn to delete dictionary entries in bulk.
Assignment: The dictionary stock keeps track of the available items in a store and their price.
The list sold0ut keeps track of which items are sold out at the end of the day. Write a code
snippet that removes , the items in stock that were sold out.
Note: all the items in soldout are a key of stock.

Solution
4.4(192 votes)

Answer

The updated dictionary is `{'banana': 1.0}`. Explanation 1. Define the dictionary and list Create a dictionary `stock` with items and their prices, and a list `soldOut` with items that are sold out. ```python stock = {'apple': 2.5, 'banana': 1.0, 'orange': 1.5} soldOut = ['apple', 'orange'] ``` 2. Remove sold-out items from the dictionary Use a loop to delete each item in `soldOut` from `stock`. ```python for item in soldOut: del stock[item] ```

Explanation

1. Define the dictionary and list<br /> Create a dictionary `stock` with items and their prices, and a list `soldOut` with items that are sold out.<br /><br />```python<br />stock = {'apple': 2.5, 'banana': 1.0, 'orange': 1.5}<br />soldOut = ['apple', 'orange']<br />```<br /><br />2. Remove sold-out items from the dictionary<br /> Use a loop to delete each item in `soldOut` from `stock`.<br /><br />```python<br />for item in soldOut:<br /> del stock[item]<br />```
Click to rate:

Similar Questions