Dictionaries · Updating & Merging Dictionaries · Practice Question
Select and Remove Keys
Question
A trader maintains a portfolio containing the following positions:
positions:`AAPL`MSFT`GOOG`TSLA`NVDA!100 250 75 50 400The trader has decided to sell all holdings in AAPL and TSLA.
Create two new dictionaries:
soldcontaining only the positions being sold (AAPLandTSLA)retainedcontaining all remaining positions
Solution
sold:`AAPL`TSLA # positions
retained:`AAPL`TSLA _ positionsResult:
q)sold
AAPL| 100
TSLA| 50
q)retained
MSFT| 250
GOOG| 75
NVDA| 400Explanation
`AAPL`TSLA # positions (Take) keeps only those two keys; `AAPL`TSLA _ positions (Drop) removes them, leaving the rest. Together they partition the portfolio into sold and retained positions.