Working with Lists#

There are couple of nice built-in functions which will be handy while we are doing our geoprocessing analysis. The first one is len() function, which returns the number of items in a list.

new_england_states = ["Rhode Island", "Massachusetts", "New Hampshire", \
                      "Maine", "Vermont", "Connecticut"]
len(new_england_states)
6

We can sort() our list : if it consists of words, it will be alphanumetical, it if it consists of numbers, it will be from smallest to largest. If you would like to reverse the order, you can use reverse():

new_england_states.sort()
print(new_england_states)
new_england_states.reverse()
print(new_england_states)
['Connecticut', 'Maine', 'Massachusetts', 'New Hampshire', 'Rhode Island', 'Vermont']
['Vermont', 'Rhode Island', 'New Hampshire', 'Massachusetts', 'Maine', 'Connecticut']

Python is a zero-indexed language and list items are indexed starting from zero. You can obtain any list item by using this index number.

print(new_england_states)
new_england_states[2]
['Vermont', 'Rhode Island', 'New Hampshire', 'Massachusetts', 'Maine', 'Connecticut']
'New Hampshire'

You can use negative index numbers to start counting from the end. The last item in the list will be returned with an index number -1.

new_england_states[-1]
'Connecticut'

Lists can be sliced into smaller lists. Slicing uses two indices separated by a colon. The first index is the number of the first element you want to include. The second index is the number of the first element you dont want to include. If you don’t provide any number, it will start from the very beginning or the end (depending on which one you are leaving out). Let’s see in examples:

print (new_england_states)
print (new_england_states[0:3])
#print (new_england_states [2:])
#print (new_england_states [:-2])
['Vermont', 'Rhode Island', 'New Hampshire', 'Massachusetts', 'Maine', 'Connecticut']
['Vermont', 'Rhode Island', 'New Hampshire']

Notice that if we call an item from a list by using the corresponding index number, it will be obtained as a string. However, if we would like to obtain as a list, we need to use the code below:

mass_string = new_england_states[3] #string
mass_list = new_england_states[3:4] # list

print(type(mass_string))
print(mass_string)

print(type(mass_list))
print(mass_list)
<class 'str'>
Massachusetts
<class 'list'>
['Massachusetts']
my_numbers = [1,2,3,4]
print(my_numbers[0])
print(my_numbers[0:1])
1
[1]

You can delete the items from a list by using del statement based on the index number:

del new_england_states[-1]
print(new_england_states)
['Vermont', 'Rhode Island', 'New Hampshire', 'Massachusetts', 'Maine']

Creating an Empty List#

You can also create an empty list and then add elements to it. This will be quite helpful if you are obtaining the elements from a function or from another source.

mylist = []
print(mylist)
mylist.append("Counties")
print(mylist)
[]
['Counties']

You can also create list of characters from a string as follows:

mypath = "https://twitter.com/GisProgramming"
letter_mypath = list(mypath)
print (letter_mypath)
['h', 't', 't', 'p', 's', ':', '/', '/', 't', 'w', 'i', 't', 't', 'e', 'r', '.', 'c', 'o', 'm', '/', 'G', 'i', 's', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']

Note that this does not work for number since they are not iterable, if you would like to conver a number to list, you must first conver the value to an iterable first. A_picture_describes_iterables

fibonacci_numbers = 112358
list(str(fibonacci_numbers))
['1', '1', '2', '3', '5', '8']

If you would like to add the number as it is, then you can create an empty list and then add the number by using append().

Commonly Used List Methods#

Now, let’s look at other commonly used list methods such as append(), count(), extend(), index(), insert(), pop(), and remove(). Check all the available methods of list objects from here.

list.append(x)#

The append() method appends an element to the end of the list

cities = ["Amherst", "Boston"]
cities.append("Newton")
print (cities)
['Amherst', 'Boston', 'Newton']

list.count(x)#

The count() method determines the number of the times an element occurs in a list:

numbers = [1,2,3,2,2,2,2,4,4,1,1,1,1]
numbers.count(1)
5

list.extend(iterable)#

The extend() method allows you to append several values at once:

list1 = [1,2,3,4]
list2 = [5,6,7,8,9,10]

list1.extend(list2)
print (list1)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

list.index(i)#

The index() method finds the index of the first occurance of a value:

new_england_states = ["Rhode Island", "Massachusetts", "New Hampshire", "Maine", "Vermont", "Connecticut"]
new_england_states.index("Maine")
3

insert()#

The insert() method makes it possible to insert an element into a list at a specified location.

new_england_states = ["Rhode Island", "Massachusetts", "New Hampshire", "Maine", "Vermont"]
new_england_states.insert(1,"Connecticut")
print (new_england_states)
['Rhode Island', 'Connecticut', 'Massachusetts', 'New Hampshire', 'Maine', 'Vermont']

list.pop()#

The pop() removes an element from a list and returns it.

new_england_states = ["Rhode Island", "Massachusetts", "Rhode Island","New Hampshire", "Maine", "Vermont"]
new_england_states.pop(0)
'Rhode Island'

list.remove(x)#

The remove() method removes the first occurance of a value, as follows:

numbers = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
numbers.remove(3)

range(start, stop, step)#

a = range(6)
b = range(2,6)
c = range(1,10,5)

print(a)
print(b)
print(c)

for item in c:
    print (item)
range(0, 6)
range(2, 6)
range(1, 10, 5)
1
6

len(object)#

len(numbers)
9

String Lists#

See Algebra of String Lists