Working with lists
You should now have some experience working with the basic datatypes in python: strings, ints, floats, and (maybe to a lesser extent) booleans. All of these datatypes store scalar values, e.g. a single value. Of course, in physics, we often deal with quantities that are described by more than one variable. 3 dimensional Vectors are one example, but you could also think of a set of temperature measurements taken periodically throughout a day as another example. For these things, it makes sense to store the data describing them with a single variable. This is the purpose of a list in Python.
[1]:
temperature = [100.0,110.2,115.4,112.3,111.8,99.8]
print(temperature)
[100.0, 110.2, 115.4, 112.3, 111.8, 99.8]
In python, lists are defined using square brackets [] and the elements of the list are separated by a comma. Python lists are highly generic. They can contain any other datatype and the datatypes within a single list don’t have to be the same:
[2]:
myList = [50.2,'Apples',42,False]
print(myList)
[50.2, 'Apples', 42, False]
Here we have a list that has 4 elements, each of a different data type.
One or more elements of a list can even be another list:
[3]:
myList = [50.2,'Apples',42,False,[5,4,8,10]]
print(myList)
[50.2, 'Apples', 42, False, [5, 4, 8, 10]]
Indexing a list
Once we have a list, it is almost always necessary to access a single value within the list. This is called indexing the list.
[4]:
print(myList[1])
Apples
The 1 in the above example is known as the index value. You can read the above line of code as: “Hey Python, please print the first element of the list called myList!”.
Note that the result of that print statement was that the string “Apples” was printed to the screen, but the “first” element of MyList was the value 50.2. So what gives? As you are probably already aware, it is common in computer programming to start counting at zero!
[5]:
print(myList[0])
50.2
which gives us the expected result.
Values vs. indicies
Properly accessing the information stored in a list is one of the trickiest things to learn as a new programmer. So, it is important to understand the difference between the values stored in a list and the index used to access a certain value.
Let’s look at another example:
[6]:
fruit = ['banana','apple','pear','watermelon','peach','apricot']
Here our list contains 6 elements. One common way to think about indexing this list is to think of an interaction with a fruit vending machine. Were to go to purchase a piece of fruit from such a machine, the machine might present you with a series of numbers that correspond to each of the fruit.
banana apple pear watermelon peach apricot
0 1 2 3 4 5
If you want to purchase the pear, you would select 2. If you want the apricot, you would select 5. You would never try to purchase the apple by selecting apple; such a button doesn’t exist and the machine would be confused!
The same goes for accessing elements of a list. You can assume that python has no idea what your list actually contains. So, if you try access the pear element of the list above using the following:
[7]:
print(fruit['pear'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-56936b90e740> in <module>
----> 1 print(fruit['pear'])
TypeError: list indices must be integers or slices, not str
python will throw a TypeError telling you that you can’t do that.
This all may be somewhat obvious, particularly when we use a list of strings as an example. But it is easy to forget the difference between list values and index values when working with numeric data, particularly if the data are integers:
[8]:
snowFall_cm = [1,0,0,4,7,5,8,0,0,4]
If we want to access that “7”, then we need to use the appropriate index value. Counting from the left, we see the “7” is the 4th value in the list. So,
[9]:
print(snowFall_cm[4])
7
gives us the expected value. Note that in this example to make things extra confusing, the 5th element of the list is also a 5.
[10]:
print(snowFall_cm[5])
5
Both are the number 5, but one is the index value 5 (5th element in the list) and one is the value 5 itself (5 cm of snowfall).
Reverse Indexing
It is often useful to count from the end of a list instead of the beginning when indexing a list. Python accommodates this with the use of negative index values:
[11]:
print(snowFall_cm[-1])
4
There is no such thing as negative zero, so the first element starting from the end of the list is indexed with -1 and then index values decrease by one from there:
[12]:
print(snowFall_cm[-4])
8
Indexing a subset of a list
Python also makes it easy to access a subset of values from within a list using the colon:
[13]:
print(snowFall_cm[2:6])
[0, 4, 7, 5]
Here we are telling python to print from the 2nd to 5th elements of our list. Python’s notation might seem a little weird here as you may have expected to see the 8 printed, which is the 6th element. Instead, you can think of this as asking python to print 6 - 2 = 4 elements, beginning with element 2.
Note that we don’t have to specify a second index to use the colon:
[14]:
print(snowFall_cm[4:])
print(snowFall_cm[:5])
[7, 5, 8, 0, 0, 4]
[1, 0, 0, 4, 7]
where in the first example we printed all elements starting at element 4 and after and in the second example we printed the first 5 elements of our list.
Note
The difference between list values and index values can’t be stressed enough. Anytime we want to access an individual element of a list, we must use the index value, the value’s position when counting from the beginning (or end if using negative values) of the list.
List methods
Let’s wrap up by discussing a couple of methods that are used often with lists.
Length of a list
The len() function allows us to determime the number of elements in a list (note that it’s a function, not a method of datatype list, because there are other datatypes that len() works with):
[15]:
print(len(snowFall_cm))
10
As expected, there are 10 items in our list.
Adding to a list
The append() method is used to add a value to a list that already exists:
[16]:
snowFall_cm.append(3)
print(snowFall_cm)
[1, 0, 0, 4, 7, 5, 8, 0, 0, 4, 3]
as you can see, we’ve added the value ‘3’ to our list and thus increased its length by 1.
[17]:
print(len(snowFall_cm))
11
Removing from a list
We can also remove items from a list using the pop() method:
[18]:
snowFall_cm.pop()
[18]:
3
will remove the last item of the list (and return that value). Alternatively, you can use pop() and specify the index that you want to remove:
[19]:
snowFall_cm.pop(6)
[19]:
8
[20]:
print(snowFall_cm)
[1, 0, 0, 4, 7, 5, 0, 0, 4]
Count occurrences within a list
Finally, the count() method will add up all of the occurrences of a specific value within a list:
[21]:
print(snowFall_cm.count(0))
4
Final note
The methods (and function) that we’ve discussed here is not at all exhaustive. There are many more ways that we can manipulate lists that haven’t been discussed here. You will certainly learn other tools as you encounter them in the wild or learn them through searching the python documentation.