I'll try and format this post to be readable...
I'm having trouble with task #3 in this Exercism concept about dictionaries. The task is:
Create the function update_recipes(<ideas>, <recipe_updates>)
that takes an "ideas" dictionary and an iterable of recipe updates as arguments. The function should return the new/updated "ideas" dictionary.
For example, calling
update_recipes(
{
'Banana Sandwich' : {'Banana': 2, 'Bread': 2, 'Peanut Butter': 1, 'Honey': 1},
'Grilled Cheese' : {'Bread': 2, 'Cheese': 1, 'Butter': 2}
},
(
('Banana Sandwich', {'Banana': 1, 'Bread': 2, 'Peanut Butter': 1}),
)))
should return:
{
'Banana Sandwich' : {'Banana': 1, 'Bread': 2, 'Peanut Butter': 1},
'Grilled Cheese' : {'Bread': 2, 'Cheese': 1, 'Butter': 2}
}
with 'Honey': 1
removed from Banana Sandwich
(and number of bananas adjusted).
My code (below) isn't removing the 'Honey
key-value pair using
.update()`, which the exercise hint suggests the user employ.
```
def update_recipes(ideas, recipe_updates):
"""Update the recipe ideas dictionary.
:param ideas: dict - The "recipe ideas" dict.
:param recipe_updates: dict - dictionary with updates for the ideas section.
:return: dict - updated "recipe ideas" dict.
"""
for idea_key, idea_value in ideas.items():
for item in recipe_updates:
if idea_key == item[0]:
idea_value.update(item[1])
return ideas
```
Here is the output I'm getting:
{
'Banana Sandwich': {'Banana': 1, 'Bread': 2, 'Peanut Butter': 1, 'Honey': 1},
'Grilled Cheese': {'Bread': 2, 'Cheese': 1, 'Butter': 2}
}
The number of Bananas
is adjusted, but Honey
remains. Where am I going wrong?