Lesson 4: Python Basics 2 - Lists#
This lesson is modified from Basic elements of Python from geo-python.
Objectives#
By the end of this lesson, you will be able to
store data in python lists
explain the concept of index in python
modify python lists
1. Lists and indices#
We learned about variables and we continue with some variables related to Meteorological observation stations in South Florida. Rather than having individual variables for each of those stations, we can store many related values in a collection. The simplest type of collection in Python is a list
.
All four data structures in python
Image Credit: bluetick.ai
1.1 Creating a list#
Let’s first create a list of selected station_name
values and print it to the screen.
station_names = [
"Homestead",
"Fort lauderdale",
"Immokalee",
"Belle Glade",
"Clewiston",
"Palmdale",
"North Port",
"Arcadia"
]
print(station_names)
['Homestead', 'Fort lauderdale', 'Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
We can also check the type of the station_names
list using the type()
function.
type(station_names)
list
Here we have a list of 8 station_name
values in a list called station_names
. As you can see, the type()
function recognizes this as a list. Lists can be created using the square brackets [
and ]
, with commas separating the values in the list.
1.2 Index values#
To access an individual value in the list we need to use an index
value. An index value is a number that refers to a given position in the list. Let’s check out the first value in our list as an example by printing out station_names[1]
:
print(station_names[1])
Fort lauderdale
Wait, what? This is the second value in the list we’ve created, what is wrong? As it turns out, Python (and many other programming languages) start values stored in collections with the index value 0
. Thus, to get the value for the first item in the list, we must use index 0
. Let’s print out the value at index 0
of station_names
below.
print(station_names[0])
Homestead
OK, that makes sense, but it may take some getting used to…
You can index more than one element as follows.
print(station_names)
print(station_names[1:4])
['Homestead', 'Fort lauderdale', 'Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
['Fort lauderdale', 'Immokalee', 'Belle Glade']
A useful analog - Bill the vending machine#
As it turns out, index values are extremely useful, common in many programming languages, yet often a point of confusion for new programmers. Thus, we need to have a trick for remembering what an index value is and how they are used. For this, we need to be introduced to Bill.
Bill, the vending machine. (Image Credit: Geo-Python)
In an analogy to Python, we could say Bill is simply a list of food items and the buttons you push to get them are the index values. For example, if you would like to buy a taco from Bill, you would push button 3
. If we had a Python list called Bill
, an equivalent operation could simply be
print(Bill[3])
Taco
Try it out. Create a list bill
with the items shown in the picture. Then print the fourth value in the list.
Ans.: taco
bill=['burger', 'cookie', 'candy','taco', 'ice cream','beer']
print(bill[3])
taco
Number of items in a list#
We can find the length of a list using the len()
function. Use it below to check the length of the station_names
list.
len(station_names)
8
Just as expected, there are 4 values in our list and len(station_names)
returns a value of 8
.
Index value tips#
If we know the length of the list, we can now use it to find the value of the last item in the list, right? What happens if you print the value from the station_names
list at index 8
, the value of the length of the list?
print(station_names[8])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[9], line 1
----> 1 print(station_names[8])
IndexError: list index out of range
An IndexError
? That’s right, since our list starts with index 0
and has 8 values, the index of the last item in the list is len(station_names) - 1
. That isn’t ideal, but fortunately there’s a nice trick in Python to find the last item in a list. Let’s first print the station_names
list to remind us of the values that are in it.
print(station_names)
['Homestead', 'Fort lauderdale', 'Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
To find the value at the end of the list, we can print the value at index -1
. To go further up the list in reverse, we can simply use larger negative numbers, such as index -8
. Let’s print out the values at these indices below.
print(station_names[-1])
Arcadia
print(station_names[-8])
Homestead
Yes, in Python you can go backwards through lists by using negative index values. Index -1
gives the last value in the list and index -len(station_names)
would give the first. Of course, you still need to keep the index values within their ranges. What happens if you check the value at index -9
?
print(station_names[-9])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[13], line 1
----> 1 print(station_names[-9])
IndexError: list index out of range
You can index the list from a certain item to the last item. Here is an example where we are indexing the list from the third to the last time
print(station_names)
print(station_names[2:])
['Homestead', 'Fort lauderdale', 'Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
['Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
If you want to index the list to the item before the last, you can index to -1
, to the item before the -2
, and so on. Here is an example where we are indexing from from the third item to the second item from the last.
print(station_names[2:-2])
['Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale']
2. Modifying list values#
Another nice feature of lists is that they are mutable, meaning that the values in a list that has been defined can be modified. Consider a list of the observation station types corresponding to the station names in the station_names
list.
station_county = [
"county",
"county",
"county",
"county",
]
print(station_county)
['county', 'county', 'county', 'county']
Let’s change the value for station_types[2]
to be "Immokalee"
and print out the station_types
list again.
station_county[2] = "Collier"
print(station_county)
['county', 'county', 'Collier', 'county']
3. Data types in lists#
Lists can also store more than one type of data. Let’s consider that in addition to having a list of each station name, ID, latitude, etc. we would like to have a list of all of the values for station “Homestead”. Before we create this list we need to define a few values related to the Homestead station as follows:
name: Immokalee
ID: 450
county: Collier
lat.: 26.46225
log.: 81.44033
Create the variables in the cell below
station_name = "Immokalee"
station_id = 440
station_lat = 25.5126
station_lon = 24.96
station_county = "Collier"
Now that we have defined some of the Immokalee variables we can create the station_Immokalee
list.
station_Immokalee = [station_name, station_id, station_lat, station_lon, station_county]
print(station_Immokalee)
['Immokalee', 440, 25.5126, 24.96, 'Collier']
Here we have one list with 3 different types of data in it. We can confirm this using the type()
function. Let’s check the type of station_Immokalee
, then the types of the values at indices 0-2
in the cells below.
type(station_Immokalee)
list
# Station name
type(station_Immokalee[0])
str
# ID
type(station_Immokalee[1])
int
# Station latitude
type(station_Immokalee[2])
float
4. Adding and removing values from lists#
Finally, we can add and remove values from lists to change their lengths. Let’s consider that we no longer want to include the first value in the station_names
list. Since we haven’t see that list in a bit, let’s first print it to the screen.
print(station_names)
['Homestead', 'Fort lauderdale', 'Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
4.1 Removing from a list#
The del
statement allows values in lists to be removed. It can also be used to delete values from memory in Python. To remove the first value from the station_names
list, we can simply type del station_names[0]
. If you then print out the station_names
list, you should see the first value has been removed.
del station_names[0]
print(station_names)
['Fort lauderdale', 'Immokalee', 'Belle Glade', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
There are several ways to remove items from lists in Python other than del
. There are two other common options you might like to know about.
station_names.remove(value)
: Will remove the first item with a value equal tovalue
station_names.pop(index)
: Will remove the item at indexindex
from the list
Try it out
station_names.remove("Belle Glade")
station_names.pop(0)
print(station_names)
['Immokalee', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia']
4.2 Adding to the list#
If we would instead like to add a few stations to the station_names
list, we can type station_names.append("List item to add")
, where "List item to add"
would be the text that would be added to the list in this example. Let’s add two values to our list in the cells below: "Fort Myers"
and "Immokalee"
. After doing this, let’s check the list contents by printing to the screen.
station_names.append("Fort Myers")
station_names.append("Immokalee")
print(station_names)
['Immokalee', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia', 'Fort Myers', 'Immokalee']
As you can see, we add values one at a time using station_names.append()
.
A list.append()
is called a method in Python, which is a function that works for a given data type (a list in this case). We will see some other examples of useful list methods below.
5. Some other useful list methods#
5.1 Count elements in a list#
With lists we can do a number of useful things, such as count the number of times a value occurs in a list or where it occurs. The list.count()
method can be used to find the number of instances of an item in a list. For instance, we can check to see how many times "Immokalee"
occurs in our list station_names
by typing station_names.count("Immokalee")
.
# The count method counts the number of occurences of a value
station_names.count("Immokalee")
2
5.2 Find the index of a value in a list#
Similarly, we can use the list.index()
method to find the index value of a given item in a list. Let’s use the cell below to find the index of "Fort Myers"
in the station_names
list.
# The index method gives the index value of an item in a list
station_names.index("Fort Myers")
5
The good news here is that our selected station name is only in the list once. Should we need to modify it for some reason, we also now know where it is in the list (index 8
). If the value occurs more than once in a list, to find indices of the value, you can use a for
loop that we will learn next.
There are two other common methods for lists that we need to see.
5.3 Reversing a list#
How to reverse a list?
How can you learn this on your own?
print(station_names)
['Immokalee', 'Clewiston', 'Palmdale', 'North Port', 'Arcadia', 'Fort Myers', 'Immokalee']
How can you learn this on your own?
You can find some resources here
First, there is the list.reverse()
method, used to reverse the order of items in a list. Let’s reverse our station_names
list below and then print the results.
station_names.reverse()
print(station_names)
['Immokalee', 'Fort Myers', 'Arcadia', 'North Port', 'Palmdale', 'Clewiston', 'Immokalee']
Yay, it works!
Caution
A common mistake when reversing lists is to do something like station_names = station_names.reverse()
. Do not do this! When reversing lists with .reverse()
the None
value is returned (this is why there is no screen ouput when running station_names.reverse()
). If you then assign the output of station_names.reverse()
to station_names
you will reverse the list, but then overwrite its contents with the returned value None
. This means you’ve deleted the contents of your list!.
5.4 Sorting a list#
The list.sort()
method works the same way. Let’s sort our station_names
list and print its contents below.
print(station_names)
['Immokalee', 'Fort Myers', 'Arcadia', 'North Port', 'Palmdale', 'Clewiston', 'Immokalee']
station_names.sort() # Notice no output here...
print(station_names)
['Arcadia', 'Clewiston', 'Fort Myers', 'Immokalee', 'Immokalee', 'North Port', 'Palmdale']
As you can see, the list has been sorted alphabetically using the list.sort()
method, but there is no screen output when this occurs. Again, if you were to assign that output to station_names
the list would get sorted, but the contents would then be assigned None
.
Note
As you may have noticed, "Fort Myers"
comes before "Fort lauderdale"
in the sorted list. This is because alphabetical sorting in Python places capital letters before lowercase letters.
Glossary:#
data type : An attribute defining the characteristics of a value in a program. For example, type int is an integer (whole number).
index : A number indicating the location of a specific value stored in Python lists or tuples. The first index value of list is always 0
.
list : A data type in Python that can be used to store collections of values. The data in lists can be heterogeneous and data can be added or removed from lists. Index values can be used to access invididual list items.
method : A method is a function that is associated with an object. Methods allow objects to perform actions or operations, typically using or modifying the object’s internal state. Methods are called on objects using the dot notation..