Back to Dictionary Arithmetic

Dictionaries · Dictionary Arithmetic · Practice Question

Key-Union Arithmetic

Sign in to track your progress

Question

You have two position dictionaries for different books:

bookA:`AAPL`MSFT`GOOG!100 250 75
bookB:`MSFT`GOOG`TSLA!200 50 300

Write a single expression to produce the combined position for every symbol across both books. For symbols held in both books, the positions should be summed; for symbols held in only one book, use that book's position.

Answer

bookA + bookB
AAPL| 100
MSFT| 450
GOOG| 125
TSLA| 300

Dictionary arithmetic automatically takes the union of both key sets. Keys present in only one dictionary contribute their value directly; keys present in both receive the arithmetic result — here, position sizes are summed.