Lists#
Basic lists#
We can create lists using square brackets. Commas separate elements in lists. Lists can contain any type of variable/data.
company1 = "APPL"
company2 = "MMM"
company3 = "MSFT"
list_of_nums = [9, 43, 75, 14, 2019]
list_of_strings = ['this', 'that', 'hello world']
list_of_variables = [company1, company2, company3]
list_of_booleans = [True, False, False, True]
list_of_mixed = [9, 'this', company1, True]
print(list_of_nums)
print(list_of_strings)
print(list_of_variables)
print(list_of_booleans)
print(list_of_mixed)
[9, 43, 75, 14, 2019]
['this', 'that', 'hello world']
['APPL', 'MMM', 'MSFT']
[True, False, False, True]
[9, 'this', 'APPL', True]
Lists can even contain other lists
list_of_lists = [list_of_nums, list_of_variables]
list_of_lists
[[9, 43, 75, 14, 2019], ['APPL', 'MMM', 'MSFT']]
We can also create empty lists.
empty_list = []
Appending to lists#
Suppose we have a list of competitor tickers, and we want to add more tickers to it.
competitors = ['AAPL', 'MSFT', 'TSLA']
more_competitors = ['AMZN', 'FB']
We could just add the lists:
all_competitors = competitors + more_competitors
all_competitors
['AAPL', 'MSFT', 'TSLA', 'AMZN', 'FB']
Another way to add to lists is to use the append method.
all_competitors.append('SNAP')
all_competitors
['AAPL', 'MSFT', 'TSLA', 'AMZN', 'FB', 'SNAP']
Indexing lists#
You can access a specific element in a list by indexing the numerical position: [#]
Here is an example using the all_competitors list.
all_competitors[1]
'MSFT'
Why does this give us the second element? Because in Python, the index starts at zero.
all_competitors[0]
'AAPL'
However, to index the last element in a list, we use -1:
all_competitors[-1]
'SNAP'
Here we access a specific value and store it in a new variable called the_one_i_want.
the_one_i_want = all_competitors[2]
print(all_competitors)
print(the_one_i_want)
['AAPL', 'MSFT', 'TSLA', 'AMZN', 'FB', 'SNAP']
TSLA
To grab a range of values, use the :
comp_subset = all_competitors[0:2]
comp_subset
['AAPL', 'MSFT']
Notice how the endpoints work. There are two elements in comp_subset, coming from list positions 0 and 1. The third element in the list (element [2]) is not included in comp_subset.
When one side of the : does not have a number, Python infers “from the beginning” or “until the end”.
print(all_competitors[:3])
print(all_competitors[-2:])
['AAPL', 'MSFT', 'TSLA']
['FB', 'SNAP']
Other list tools#
Just like .append(), there are other functions (usually called methods) that we can use with lists.
For example, we can remove elements from a list and store them in a new variable using the .pop() method. The following code removes the last item from the list and stores it in the x variable.
list_of_nums = [9, 43, 75, 14, 20, 19, 54, 98, 4]
x = list_of_nums.pop()
print(x)
print(list_of_nums)
4
[9, 43, 75, 14, 20, 19, 54, 98]
Similarly, we can remove the first element (remember indexing starts at 0), third element, etc.
y = list_of_nums.pop(0)
z = list_of_nums.pop(2)
print(y)
print(z)
print(list_of_nums)
9
14
[43, 75, 20, 19, 54, 98]
Just remember that after every .pop() reference the list is shortened.
We can also sort lists using the .sort() method. This modifies the list in place. It is not creating a new list.
list_of_nums.sort()
list_of_nums
[19, 20, 43, 54, 75, 98]
If we want to know how many items there are in a list, we can use the len() function:
list_of_nums = [9, 43, 75, 14, 2019]
list_length = len(list_of_nums)
list_length
5