Skip to content

Data structure

How data structures preserve identity and order, support useful operations, and affect the correctness and cost of quant calculations.

A data structure is a way of organising data together with operations for accessing and changing it. In quant work, the choice determines how a program preserves instrument identity, observation order and relationships between values. A fast calculation can still be wrong if it pairs a holding with another instrument's price.

Start with the operation

Separate the relationship you need from the representation that implements it. A mapping associates each key with a value; a Python dict implements that relationship using a hash table in CPython. A queue specifies first-in, first-out behaviour; it can be implemented using different storage arrangements. An algorithm uses these operations to perform a task.
Question the program asksUseful relationshipPossible representation
What was the next observation?Ordered sequencePython list
What quantity belongs to this instrument?Key-to-value mappingPython dictionary
Is this instrument eligible?Unique membershipPython set
Choose after deciding whether duplicates matter, what order means and whether values will change. A numerical array supports another useful arrangement, but its positions need an explicit interpretation such as date or instrument. No container supplies that meaning automatically.

Identity before position

Consider fictional holdings of 2 units of A and 3 units of B. At one valuation date, marks are USD 100 per unit of A and USD 50 per unit of B. Assume these marks are the chosen valuation inputs, with no currency conversion or transaction costs.

holdings = {"A": 2, "B": 3} marks_usd = {"B": 50, "A": 100} value_usd = sum( quantity * marks_usd[instrument] for instrument, quantity in holdings.items() ) print(value_usd) # 350
The dictionary lookup matches each quantity to its instrument: A contributes USD 200 and B contributes USD 150. The result is USD 350 even though the two dictionaries have different insertion orders. Python dictionaries preserve insertion order; that order is neither a sort order nor a safe alignment rule between separate dictionaries. Python documents both the key operations and the order guarantee.
Fictional holdings A: 2 units and B: 3 units. Keyed prices A: USD 100 and B: USD 50 give USD 350. Pairing quantities with reversed price positions gives the incorrect USD 400.
Fictional holdings A: 2 units and B: 3 units. Keyed prices A: USD 100 and B: USD 50 give USD 350. Pairing quantities with reversed price positions gives the incorrect USD 400.
The same numbers produce different totals when instrument identity is lost. Prices and quantities are fictional, for one valuation date and one currency.

Pairing the raw value sequences by position would instead calculate 2 × 50 + 3 × 100 = USD 400. Matching by key prevents this particular mistake. It does not establish that the marks are current, executable or appropriate for valuation.

A missing mark raises KeyError in this example; silently substituting zero would impose a different valuation policy. Each dictionary key holds one value: assigning a second quantity to A replaces its previous quantity rather than accumulating another trade. An event history needs a representation that retains repeated events, followed by an explicit aggregation rule. See mutable state for the consequences of changing stored values.

Cost belongs to an operation

Big-O notation describes how cost grows with input size. For exact built-in types in CPython, list indexing is constant time, while searching an unsorted list is linear in its length, assuming constant-cost comparisons. Dictionary lookup is average-case constant time with well-distributed hashes and constant-cost hashing and equality checks. Collision-heavy worst cases can be linear. These are operation and implementation assumptions, rather than a promise that a dictionary is always faster. Python's complexity reference states the qualifications.
For a practical next step, Collections teaches the sequence, mapping and membership choices with Python examples, including shared mutable objects. The full primer lesson is free with sign-in; a public preview is available.

Sources

The linked reference pages are the Python 3.14 documentation; the example was executed with CPython 3.14.4.

Last updated
  • programming
  • data structures
  • python
  • data integrity
← Back to the glossaryBack to top ↑