Subsubsection A.3.1.1 Indexing and slicing
The index of an element of a list is the distance in the list from that element to the start of the list. The element at the start of the list is thus in index 0, the next element in index 1, the next in index 2, and so on. Thus a list consisting of 10 elements has indices 0 through 9. For a list the_list
, we access the element in index \(i\) by using the_list[i]
; this is similar to indexing a mathematical sequence as \((a_0,a_1,a_2,\dotsc,a_k)\text{.}\)
>>> the_list = ['bob', 'larry', 3.14159, 1-1j, 'fifth']
>>> the_list[0]
'bob'
Unlike a mathematical sequence, Python allows indexing from the back of the list as well. When indexing from the back of the list, it is important to remember that the same principle applies as above: the negative index is the distance from the “front” of an element to the back of the list. Continuing the above, we have:
>>> the_list[-2]
(1-1j)
>>> the_list[-1]
'fifth'
Since \(0 = -0\text{,}\) the negative zero index is the same as the zero index:
>>> the_list[-0]
'bob'
>>> the_list[0]
'bob'
To access a sublist of a list, we use a slice. The first number of the slice is the index of the element to start the sublist, and the second number of the slice is the index of the first element not to include. To slice starting at index 1 and stopping before index 5 of the_list
, we would use the_list[1:5]
. Since the_list
only had five elements we will define a longer list to experiment.
>>> new_list = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
... 610, 987, 1597, 2584, 4181, 6765, 10946]
>>> new_list[1:5]
[2, 3, 5, 8]
You can slice by skipping indices, as well, by supplying a third term.
>>> new_list[1:15:2]
[2, 5, 13, 34, 89, 233, 610]
Finally, if you want to slice from the beginning of the list up to the \(j\) th index, you use new_list[:j]
. If you want to slice from the \(i\) th index to the end of the list, you use new_list[i:]
. If you want to slice a whole copy of a list (there are valid reasons to do this), use new_list[:]
. If you want the reverse of a list, you slice the whole thing from beginning to end using a skip of negative one: new_list[::-1]
.
Subsubsection A.3.1.2 Operators and methods
Indexing and slicing are just the beginnings of what can be done with lists. We can also combine two lists into a longer one (additively) or elongate a list by making it contain several copied of itself (multiplicatively). Continuing the above examples, we have:
>>> the_list + new_list
['bob', 'larry', 3.14159, (1-1j), 'fifth', 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]
>>> 3 * the_list
['bob', 'larry', 3.14159, (1-1j), 'fifth', 'bob', 'larry', 3.14159, (1-1j), 'fifth', 'bob', 'larry', 3.14159, (1-1j), 'fifth']
These are operators which we are used to working with algebraically, now given new meaning to interact with lists. Methods are different: they are functions specific to the list
data type accessed using dot notation. When working with a new data type, a good way to see the full list of all of its methods is to ask for help
, but beware! The help
files are very detailed. Try help(list)
at the Python prompt.
Many of the methods (all of those named __likethis__
) are special. We will discuss those later in the text when we discuss operator overloading in [provisional cross-reference: sec-overloading]
. The list
class has several other methods as well. Assuming that we are using the_list
from above, here is a basic overview of several of the more useful methods: