List Comprehension

For each element in the given list, Python applies certain commands to that item. These commands are the lines of code that appear within a for loop (the indented section of code that follows the for statement.

When the for loop operation is only a single line long, there is a special shortcut that we can use. This shortcut is called list comprehension. Consider the task of subtracting the risk free rate from the list of daily returns in order to compute an excess return. For simplicity, assume the risk free rate is constant and equal to \(0.01\). One way of achieving this is with the following for loop.

daily_stock_returns = [0.03, 0.01, -0.02, 0.01, -0.01]
excess_returns = []
for i in range(5):
    excess_returns.append( daily_stock_returns[i] - 0.01 )
print(excess_returns)
[0.019999999999999997, 0.0, -0.03, 0.0, -0.02]

Before showing off list comprehension, let’s stop and think about one potential issue with the above code. In the for loop above, we explicity refer to the number \(5\) when defining the range of the loop. That’s a bad idea. What if we add more stock return data to our list, and then re-run the above code? In that scenario, the for loop won’t execute as intended, since it will still only calculate excess returns for the first five days.

A better way to loop over a list is to use the len() function (‘len’ stands for ‘length’, but Python is nice and only expects us to type out three characters instead of six). The function len() can calculate the length of a list.

len(daily_stock_returns)
5

Hence, we improve upon our for loop above by removing the explicit reference to the number \(5\).

excess_returns = []
for i in range( len(daily_stock_returns) ):
    excess_returns.append( daily_stock_returns[i] - 0.01 )
print(excess_returns)
[0.019999999999999997, 0.0, -0.03, 0.0, -0.02]

Now, the above code will work as expected, regardless of how many items are in the daily_stock_returns list.

An alternative to using len() and referencing list elements by their index position (i.e. doing daily_stock_returns[i]) is to simply tell Python that we want to loop over daily_stock_returns. Thus, rather than telling Python we want to loop over range(5) or over range( len(daily_stock_returns) ), we can simply tell Python to loop over daily_stock_returns directly.

for i in daily_stock_returns:
    print(i)
0.03
0.01
-0.02
0.01
-0.01

Thus, an alternative improvement to the for loop is to do the following.

excess_returns = []
for i in daily_stock_returns:
    excess_returns.append( i - .01 )
print(excess_returns)
[0.019999999999999997, 0.0, -0.03, 0.0, -0.02]

If you don’t need to use the list index position inside the for loop (as is the case here), it’s better to loop over the list directly, rather than a list of numbers coresponding to the index positions. The reason is that this form of the for loop is the cleanest. It requires the least amount of typing, and it is the easiest to read.

Now that we’ve seen that we can loop over a list directly (rather than looping over index positions for the list), we can explain list comprehension. List comprehension takes a simple for loop (with one line of code inside the loop) and converts the loop into a neater, shorter piece of code. Consider the following example.

[i for i in range(3)]
[0, 1, 2]

The statment is wrapped up in square brackets, indicating a list variable. Within these square brackets, we tell Python to add to this list the value i, where we define i as any element in range(3).

Likewise, if we allow i to be any element in daily_stock_returns:

[i for i in daily_stock_returns]
[0.03, 0.01, -0.02, 0.01, -0.01]

Rather than store just the value i, we could store some function of it. So, returning to the earlier example using range(3)

[i**2 for i in range(3)]
[0, 1, 4]

the square of each value for i is included in a list.

In the case of daily stock returns, we can substract the risk free rate to get the excess return.

[i - .01 for i in daily_stock_returns]
[0.019999999999999997, 0.0, -0.03, 0.0, -0.02]

Hence, the easiest way to compute excess returns is with the simple, one-line statement:

excess_returns = [i - .01 for i in daily_stock_returns]
print(excess_returns)
[0.019999999999999997, 0.0, -0.03, 0.0, -0.02]

Because list comprehension is just a shortcut to writting out a simple for loop, a person could use Python perfectly fine without knowing how to use list comprehension. However, it’s a popular shortcut, so we highlight it here because it will be used throught this text.

Concept check: Using list comprehension, iterate over range(2,11,2) and obtain the list of values [1, 2, 3, 4, 5] by dividing each item in the range by 2.