In the same way that a paper shopping list holds many ‘types’ of shopping in one place, a Python list holds many ‘types’ of data in one place.
myList = [1, 3, 5, 7] # homogenous list
myList = [1, "dog", 7.01] # heterogenous list
myList = [] # empty list
Python lists are always recognisable by their “square brackets”: [...]
In fact, when I say lists can hold many types of data, I should have said that they can hold any type of data:
The output of print(a)
is:
Lists are ‘indexed’ numerically from the zero-th element:
geographers [ | 0 | 1 | 2 | ] |
---|---|---|---|---|
Massey 1 | Harvey 2 | Rose 3 |
We can also use variables as list indexes:
Anything that evaluates (i.e. resolves) to a number can be used as an index:
We can ‘count’ backwards from the end of the list using negative numbers:
Errors can be scary… but informative!
IndexError: list index out of range
And then try:
TypeError: list indices must be integers or slices, not float
Notice that Python gives us important hints about the source of the problem!
You can access more than one element at a time using a slice:
geographers = ["Massey", "Harvey", "Rose"]
print( geographers[0:2] ) # ['Massey','Harvey']
print( geographers[1:] ) # ['Harvey', 'Rose']
print( geographers[-2:] ) # ['Harvey', 'Rose']
The syntax for a slice is: list[ <start_idx>, <end_idx> ]
, but end_idx
is not included in the slice. And notice:
What do you think this will produce?
See if you can work out in your head before typing it!
list.index(...)
tells you where something can be found in a list:
geographers = ["Massey", "Harvey", "Rose"]
geographers.index("Harvey") # 1
geographers.index("Massey") # 0
Combining ideas that will become very useful later:
What do you think this prints? Why does it work at all?
list.index(...)
has one flaw:
geographers = ["Massey", "Harvey", "Rose"]
geographers.index('Batty')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'Batty' is not in list
If ‘throwing an error’ is overkill, then here’s another way:
We can sort lists in alpha-numerical order:
geographers = ["Massey", "Harvey", "Rose"]
geographers.sort()
print(geographers) # ['Harvey', 'Massey', 'Rose']
And we can reverse-sort too:
Mutable == “liable or subject to change or alteration”
Let’s replace Rose with Jefferson1 in the list.
When we insert()
items into, or pop()
items out of, a list we normally need to specify the index.
geographers = ["Massey", "Harvey", "Jefferson"]
geographers.insert(0,"von Humboldt")
print(geographers)
# ['von Humboldt', 'Massey', 'Harvey', 'Jefferson']
geographers.insert(3,"von Humboldt")
print(geographers)
# ['von Humboldt', 'Massey', 'Harvey', 'von Humboldt', 'Jefferson']
And in ‘reverse’:
There are two ways to remove David Harvey from the list of geographers without writing this:
geographers = ['von Humboldt', 'Massey', 'Harvey', 'Jefferson']
geographers.pop(2) # Do not use this answer!
We combine lists using addition:
Note that this is not the same!
female_geographers = ['Rose','Valentine','Massey','Jefferson']
male_geographers = ['Von Humboldt','Harvey','Hägerstrand']
all_geographers = []
all_geographers.append(female_geographers)
all_geographers.append(male_geographers)
print(all_geographers) # [['Rose',...], [..., 'Hägerstrand']]
print(all_geographers[0]) # ['Rose', ..., 'Jefferson']
What do you think has happened here?
male_geographers = ['Von Humboldt','Harvey','Hägerstrand']
male_geographers.append('Batty')
print(male_geographers)
What do you think this will produce? And why do you think that append
appears to do something different in these two examples?
len(...)
gives you the length of ‘countable’ things:
But…
geographers = ["Massey","Harvey","Rose"]
print("Massey" in geographers) # True
print("Batty" in geographers) # False
But…
is a ValueError
that causes your Python code to fail.
Why might you choose one of these over the other?
How would you change this code:
geographers = ["Massey","Harvey","Rose"]
print("Massey" in geographers)
print("Batty" in geographers)
So that it prints:
You will have seen the answer to this in Code Camp, but you can also Google it†!
Because they come up a lot in geo-data, it’s worth knowing about tuples, which are basically immutable lists:
t = (52.124021, -0.0012012)
print(type(t)) # <class 'tuple'>
print(t) # (52.124021, -0.0012012)
print(t[0]) # 52.124021
But this…
will throw an error:
TypeError: ‘tuple’ object does not support item assignment
Lists • Jon Reades