Back to Updating & Merging Dictionaries

Dictionaries · Updating & Merging Dictionaries · Practice Question

Select and Remove Keys

Sign in to track your progress

Question

A trader maintains a portfolio containing the following positions:

positions:`AAPL`MSFT`GOOG`TSLA`NVDA!100 250 75 50 400

The trader has decided to sell all holdings in AAPL and TSLA.

Create two new dictionaries:

  1. sold containing only the positions being sold (AAPL and TSLA)
  2. retained containing all remaining positions

Solution

sold:`AAPL`TSLA # positions

retained:`AAPL`TSLA _ positions

Result:

q)sold
AAPL| 100
TSLA| 50

q)retained
MSFT| 250
GOOG| 75
NVDA| 400

Explanation

`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.