# Lists¶

A Python list holds a list of items in an ordered way. For example, a list of favorite things might include

favorite_things1 = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles', 'warm woolen mittens']


for Julie Andrews.

When we say lists are ordered, it’s because we can enter favorite_things1 and get back 'raindrops on roses' as Julie Andrew’s first favorite thing. Remember, Python starts counting at zero!

Lists can hold whatever you want. It could be strings (as shown above), numbers, or even other variables. Consider a list of daily stock returns

daily_stock_returns = [0.03, 0.01, -0.02, 0.01, -0.01]


Let’s input this into the following code block for use throughout this section.

daily_stock_returns = [0.03, 0.01, -0.02, 0.01, -0.01]


If we want the stock return for day 0, we simply type

daily_stock_returns

0.03


and Python gives us back the value. Now suppose that the five daily stock returns corresponded to a week of stock return data. How do we convert this data into a weekly return?

$\text{weekly return} = (-1) + \Pi_{t=1}^5 (1 + \text{daily return}_t)$

In case the $$\Pi$$ symbol is unfamiliar, it means multiply. Just like $$\sum_{i=1}^3 x_i$$ means $$x_1 + x_2 + x_3$$, the statement $$\Pi_{i=1}^3 x_i$$ means $$x_1 * x_2 * x_3$$.

The simple way to do this is

(-1) + (1+daily_stock_returns) * (1+daily_stock_returns) * (1+daily_stock_returns) * (1+daily_stock_returns) * (1+daily_stock_returns)

0.019392050600000044


That looks annoying to type out, doesn’t it? Now imagine calculating an annual return from 252 days of daily trading data. This would be prohbitively tedious.

Luckily, we’re learning how to program, which means we can take advantage of useful things like for loops to make our lives easier.

weekly_return = 1
for i in range(0,5):
weekly_return = weekly_return*(1+daily_stock_returns[i])
weekly_return -= 1
print(weekly_return)

0.019392050600000044


Stop to think for a moment about why we set weekly_return equal to $$1$$ before beginning the for loop.

Most, if not all, variable types in Python have special functions that exist for those variable types. For example, string variables contain text, and so these variables have .upper() and .lower() functions that can be used with them.

my_str = 'Finance Data Science'
print(my_str.upper())

FINANCE DATA SCIENCE

print(my_str.lower())

finance data science


For list variables, two important functions are .append() and .pop(). These functions add and remove data from the list, respectively.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

[1, 2, 3, 4]


The .append() function takes as an argument the data that you want to append to a list, and then adds that data to the end of an existing list. Note that, as with .upper() or .lower() for strings, here the function starts with a . and follows the variable name. This is different than using a function like print() or, from earlier in this chapter, wacc(). The difference exists because .append() is a function defined specifically for the list variable type.

my_list = [1, 2, 3]
my_list.pop()
print(my_list)

[1, 2]


.pop() removes data from a list, and you don’t necessarily need to say what data gets deleted. By default, if you do not supply an argument to .pop() then Python will delete the last item from the list. To specify an element to delete, select it based on that element’s position in the list.

my_list = [1, 2, 3]
my_list.pop(0)
print(my_list)

[2, 3]


In Python, for loops operate on lists of information. For example, the command for i in range(5) tells Python to operate on the list of numbers from $$0$$ to $$4$$. Likewise, for i in ['cat', 'dog', 'bird'] tells Python to operate on the list of strings ‘cat’, ‘dog’, and ‘bird’. Notice, therfore, that the list of items we loop over does not have to use numbers! Let’s see what happens when we loop over these strings.

for i in ['cat', 'dog', 'bird']:
print(i)

cat
dog
bird


The value for i in the above for loop is set to be each of these strings in turn.

Concept check: Lists can be empty, meaning that they have no items inside them. For example x = [] is an empty list because it has no items. In the code cell below, start with an empty list called squares. Then, looping over range(5) with a loop index of i, append the value $$i^2$$ to the list squares.`