Quant500 Academy

3. Data structures: strings, lists, tuples and dictionaries

Level 2: Python from Scratch for Finance

Beyond the basic types, Python has four structures you will use in practically any financial script: strings, lists, tuples and dictionaries.

A string is text — a stock's ticker, the name of a strategy. It can be indexed as if it were a list of characters, and here comes the first detail that surprises anyone coming from Excel or MATLAB: in Python, indexing starts at 0, not at 1.

ticker = "AAPL"
ticker[0]      # 'A' - the first character, index 0
ticker[0:2]    # 'AA' - from index 0 up to index 2 (not included)
len(ticker)    # 4 - number of characters

A list is an ordered, modifiable collection of elements, and it is probably the structure you will use most. It is created with square brackets, and unlike an array in other languages, it can mix different types within the same list — although in practice, for financial data, you will almost always want all elements to be of the same type.

week_prices = [182.52, 184.10, 179.88, 181.45, 183.20]

week_prices[0]      # 182.52, the first price
week_prices[-1]     # 183.20, the last price (negative indices count from the end)
week_prices[1:3]    # [184.10, 179.88], a slice of the list

You can add elements with append, and lists are mutable: you can change any element by assigning it a new value directly through its index.

week_prices.append(185.00)   # adds a new price at the end
week_prices[0] = 182.00      # corrects the first price

A tuple is like a list, but immutable: once created, you cannot modify its elements. It is typically used to group data that belongs together and should not change by accident, such as a pair of coordinates or, in trading, something like the pair (ticker, entry_date) of a trade.

trade = ("AAPL", "2026-01-15")
ticker, date = trade   # unpack the tuple into two variables

trade[0] = "MSFT"   # this raises an error: tuples cannot be modified

A dictionary stores key-value pairs, and it is the ideal structure when you need to look up a piece of data by name rather than by position — for example, storing the latest price of several tickers at once.

latest_prices = {
    "AAPL": 182.52,
    "MSFT": 415.30,
    "GOOGL": 178.90
}

latest_prices["AAPL"]          # 182.52
latest_prices["TSLA"] = 245.10 # adds a new entry to the dictionary

Choosing between a list, a tuple and a dictionary is not just a matter of taste: a list for a sequence of prices over time, a tuple for a compound piece of data that must not change, and a dictionary to look up values by name — using the wrong structure usually ends in code that is longer and slower than it needs to be.

The rest of Quant500

What you are learning, applied to the index every day:

The model portfolio, weekly in your inbox

Every Saturday, its week in one minute: how it went, the best and the worst, and which insiders bought with their own money. Plus, the day the model is rebuilt, which stocks come in and which go out. A simulation, not advice. At most one email a week, no ads, no account; one-click unsubscribe. Made by one person: Pol Gracia.

A model portfolio with simulated results, not a personalized recommendation. Your email is used for this only: privacy.

Behind the Academy and the rest of Quant500 there is one person: Pol Gracia Serrahima.