GIS Logo GSP 118 (318): GIS Programming

Lists

In Review

1. Lists

Lists can be thought of just as a "list" you might create such as a shopping list or a do to list. Lists are very commonly used for many applications and we'll need them to use "for" loops.

A list can be created by setting a variable equal to a comma-separate list of "elements" surrounded by brackets. The example below sets "TheList" to contain some different types of plants and then prints out the list. Pythons "print()" function is very flexible and will do its best to print out whatever you put in the parenthesis. When you run the program, you should see the names of the plants appear in the "Debug I/O" tab with brackets around them. The brackets are important because they indicate that you have printed a list.

TheList=['tree', 'shrub', 'grass', 'fern']
print(TheList)

Set a break point at the first line of the program and then run the program. Single step over the first line and then examine what is in the "Stack Data". You'll see "TheList" and can open it to see the elements within the list. Notice that they are "indexed" by values 0, 1, 2, and 3.

You can add elements to a list in the same way we added content to a string (because strings are very similar to lists, but more on that later). In the example below "['lichen']" creates a new list and then adds it to our existing list.

TheList=TheList+['lichen']

Lists can also contain numbers or any combination of strings and numbers and even other types of data including other lists.

2. List Indexing

Python provides the ability to "index" the values within the list with integer values. Remember that lists always start with "0" rather than "1" as the first element in the list.

If you've worked with other programming languages, you'll see that indexing lists looks a lot like indexing an array. There is a strong similarity but Lists in Python are more flexible than arrays in other languages.

A single value within brackets after the list will return the value within the list. Try the following:

TheList=['tree', 'shrub', 'grass', 'fern']
print(TheList[0])
print(TheList[3])
print(TheList[4])

On the last line you should have received an error that the index '4' is "out of bounds". Even though the list has 4 elements, because it started at 0, the last index is 3.

You can add a column (":") after the index and then the index to another element to create a new list with a subset of the original list. The second index will be the one AFTER the last element copied. Try the example below:

TheList=['tree', 'shrub', 'grass', 'fern']
Subset=TheList[1:3]
print(Subset)

Notice that we were able to print the entire string in one call to "print()". The "print()" function will try to print just about anything you send to it although the display may not be as friendly as you would like. You can always fix this by putting a "for" loop around the "print()" and customizing the formatting for the list.

You can also use the indexes to insert elements into a list. Try the following

TheList=['tree', 'fern']
TheList[1:1]=['shrub', 'grass']
print(TheList)

Indexes can be used to remove items from a list. Just set an empty list to a range of index values as follows:

TheList=['tree', 'fern','shrub', 'grass']
TheList[1:3]=[]
print(TheList)

Lastly, you can use brackets and a colon without indexes to create a copy of an entire list.

TheCopy=TheList[:]

3. List Functions

There are a number of functions for lists that you may find handy.

Function Description
Index=TheList.index(TheElement) Returns the first index of the element in the list
NumElements=len(TheList) Returns the number of elements in the list
NumElements=TheList.count(TheElement) Returns the number of times the element appears in the list
TheList.sort() Sorts the contents of the list
TheList.append(TheElement) Adds an element to the end of a list

 

4. Range Function

The "range()" function is very handy to create lists of values. The first parameter is the starting value while the second parameter is the number of values to create. Try the code snippet below to see how to create a range of values from 0 to 100

TheRange=range(0,100)
print(TheRange)

Notice that the range goes from 0 to 99. Because we started at 0 instead of 1 this is a range containing 100 values.

You can add a third parameter to set the value to "step" between each value. Try the following:

TheRange=range(0,100,3)
print(TheRange)

Ranges are very handy to create "'for" loops and other places you'll want to have a sequence of numbers.

Additional Resources

Python Documentation: Lists

Python Documentation: Data Structures