Dictionaries · Dictionary Arithmetic · Practice Question
Key-Union Arithmetic
Question
You have two position dictionaries for different books:
bookA:`AAPL`MSFT`GOOG!100 250 75
bookB:`MSFT`GOOG`TSLA!200 50 300Write 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 + bookBAAPL| 100
MSFT| 450
GOOG| 125
TSLA| 300Dictionary 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.