Skip to content
Course contents
The core of the language

A sequence of things

A list holds an ordered sequence, a run of closing prices, a set of tickers, and lets you add to it, read from it, and slice pieces out. It is the first container, and the workhorse of early programs.

7 min readChapter 7 of 30
What you will learn
  • Create a list and access items by index
  • Slice and modify a list
  • Build a list of prices or symbols

A single variable holds one value, but trading is full of sequences: a week of closing prices, a watchlist of tickers, a series of daily returns. Python's tool for an ordered sequence of values is the list, and it is the workhorse container of your early programs. Once you can put many values in a list and get them back out, you can start doing the repetitive work that made coding worth learning.

Making and reading a list

A list is an ordered sequence you index from zero and slice with [start:stop]: here five closing prices, with prices[1:3] picking two of them.
A list is an ordered sequence you index from zero and slice with [start:stop]: here five closing prices, with prices[1:3] picking two of them.

You create a list by putting values between square brackets, separated by commas, and you read a value back by its position, called its index. This program stores a week of closing prices and pulls pieces out of it.

ExampleCreating, reading, slicing, and growing a listch07/lists.py
# A list holds an ordered sequence: here, a week of closing prices.
closes = [1380, 1402, 1395, 1410, 1425]

print("Days recorded:", len(closes))
print("Monday close:", closes[0])      # first item, index 0
print("Friday close:", closes[-1])     # last item
print("First three:", closes[:3])      # a slice

closes.append(1440)                    # add another day
print("After append:", closes)
print("Highest:", max(closes), "Lowest:", min(closes))
Output
Days recorded: 5
Monday close: 1380
Friday close: 1425
First three: [1380, 1402, 1395]
After append: [1380, 1402, 1395, 1410, 1425, 1440]
Highest: 1440 Lowest: 1380

Work through the output. len(closes) reports 5, the number of items. The first item is closes[0], giving 1380, because Python counts positions from zero, not one, which surprises every beginner exactly once. The last item is closes[-1], giving 1425, since negative indices count from the end. The slice closes[:3] returns a new list of the first three items, [1380, 1402, 1395], a piece cut from the whole.

Changing a list, and asking about it

Lists can grow and change. closes.append(1440) adds a value to the end, so the list becomes six items long, as the next line of output shows. And Python's built-in functions answer questions about a whole list at once: max(closes) finds the highest value, 1440, and min(closes) the lowest, 1380, without you writing any comparison yourself.

Two ideas here will carry a long way. First, zero-based indexing: the first item is at index 0, the second at 1, and so on, so a list of five items has valid indices 0 through 4. Reaching past the end, asking for closes[5] here, is a common beginner error that Python flags with an IndexError. Second, a list is the natural shape for market data before you meet the specialised tools later in the course: a column of prices, a set of symbols, a run of returns all start life as lists, and the operations here, indexing, slicing, appending, and the max and min of a series, are exactly what you will do to real prices.

What to carry forward

A list is an ordered sequence of values in square brackets: you read items by index (from 0, with negatives counting from the end), slice pieces with a colon, grow it with append, and summarise it with len, max, and min. It is the natural home for a run of closing prices or a watchlist of tickers, and the operations here are what you will do to real market data.

A list is indexed by position, but often you want to find a value by name instead, a ticker's price, say. The next chapter introduces the dictionary, which maps names to values.