Professional Documents
Culture Documents
Lesson 2.4 Lists: ITC Faculty of Geo-Information Science and Earth Observation University of Twente
Lesson 2.4 Lists: ITC Faculty of Geo-Information Science and Earth Observation University of Twente
Fundamentals
Stacks, queres and matrices
Iterations
Summary
Lesson 2.4
Lists
2010
Objectives
Structure
This lecture is structured into the following six parts, including this
introduction:
Introduction
Basics
Stacks, queries, Matrices
Iterations
Lists are objects
Summary
Expected results
Prerequisites
Lists [...]
>>> a
['spam', 'eggs', 100, 1234]
The items need not all have the same type. Notice that the list above
contains a mix of strings and integers.
List index
Like a string index, a list index always starts at 0. Just like strings, lists
can also be sliced, concatenated and so on.
Negative indices
A list index can also have a negative value. A useful way to envisage this
is that there are in fact two ‘mirror’ copies of any list:
[ ' spam ' , ' eggs ' , ' bacon ' ][ ' spam ' , ' eggs ' , ' bacon ' ]
-3 -2 -1 0 1 2
>>> a
['spam', 'eggs', 100, 1234]
>>> a
['spam', 'eggs', 123, 1234]
List length
>>> len(a)
8
We should note that it is also possible to get the length of individual list
elements. For example, what is the value of len(a[0])?
Empty lists are useful at the start of iterations when the result will be a
list.
>>> a = [ ] # create a list with no elements
>>> len(a)
0
>>> a = list()
>>> len(a)
0
The in keyword
We can use the in keyword for lists the same way we do for strings to find
whether or not a list contains a certain value.
>>> x = [1, 2, 3, 4, 5]
>>> 1 in x
True
>>> 100 in x
False
Nested lists
>>> q = [2, 3]
>>> p = [1, q, 4]
>>> len(p)
3
>>> p[1]
[2, 3]
>>> p[1][0]
2
>>> p[1].append('xtra')
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q
[2, 3, 'xtra']
List examples
The list methods make it very easy to use a list as a stack, where the last
element added is the first element retrieved. This is known as “last-in,
first-out” or LIFO).
To add an item to the top of the stack, use the append() method. To
retrieve an item from the top of the stack, we use the pop() method without
an explicit index.
List as queue
You can also use a list conveniently as a queue, where the first element
added is the first element retrieved. This is known as “first in, first out” or
FIFO).
To add an item to the back of the queue, use append(), and to retrieve an
item from the front of the queue, use pop(0) with 0 as the index.
>>> queue = [ " Eric " , " John " , " Michael " ] # The queue
>>> queue . append ( " Terry " ) # Terry joins the queue
>>> queue . append ( " Graham " ) # Graham joins the queue
Queue or FIFO
>>> queue
['Michael', 'Terry', 'Graham']
The del statement offers a way to remove an item from a list given its index
instead of its value. It can also be used to remove slices from a list.
For example:
In some cases there are even options, as in the remove() method. However
this will remove the first instance of the value in a list, not necessarily the
one you might want to remove. However it would work in this case.
Note we can also remove slices!
Deleting variables
>>> del a
Referencing the name a hereafter is an error (at least until another value
is assigned to it):
>>> a
NameError: name 'a' is not defined
In fact, the del statement is even more powerful than illustrated here, so be
careful when using it.
Lists as matrices
Earlier in this lecture it was said that lists were a very flexible datatype.
Suppose we wanted to work with matrices. We could create a list of lists:
>>> a
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> a[0]
[1, 2, 3]
>>> a[0][0]
1
>>> a[0][1]
2
...
Cloning a list
>>> a = [ 0, 1, 4, 9, 16, 25 ]
>>> a.append(36)
>>> a
[0, 1, 4, 9, 16, 25, 36]
>>> b
[0, 1, 4, 9, 16, 25]
Iterations on Lists
>>> l = [ 0, 1, 4, 9, 16, 25 ]
>>> i = 0
>>> while i < len(l):
print l[i],
i += 1
0 1 4 9 16 25
>>> l = [ 0, 1, 4, 9, 16, 25 ]
>>> for e in l:
print e,
0 1 4 9 16 25
Summary on Lists
Tuples (...)
Like a List, but immutable!
Sequence data type
Dictionaries {...}
Like a list, but uses key
Iterable, but not a sequence data type!
>>> set(a)
set([66.25, 1, 333, 1234.5])
Follow up