Editor

Merge Two Inventories

Beginner · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Two shops store stock as dictionaries of item -> count. Write merge_inventory(a, b) that returns a new dictionary with the counts added together. Do not change the inputs.

merge_inventory({"apple": 3, "pear": 1}, {"apple": 2, "fig": 5})
->  {"apple": 5, "pear": 1, "fig": 5}
def merge_inventory(a, b):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
merge_inventory({"apple": 3, "pear": 1}, {"apple": 2, "fig": 5}){'apple': 5, 'pear': 1, 'fig': 5}
merge_inventory({}, {"x": 1}){'x': 1}

More practice problems

All Python exercises · 50 practice problems