[Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Aug 24 01:03:41 CEST 2005


Hi Tom,

Before we continue: it looks like you're starting to learn Python.  Have
you gone through one of the tutorials here?

    http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Alan Gauld's tutorial is especially nice, but all of the tutorials there
should be useful.  If you go through any one of them, each should help
address the issues you're running into.


Ok, let's look at the program.  I'll try to make constructive criticism
along the way.

> input = open('/home/tom/Python/Input/SPY3.txt', 'r')
> N=0
> s = 'boo'
> date =['hoo']
> T = [0,0,0,0,0,0]               #don't know why this is necessary

There are an awful number of global variables here, and their names do not
make it clear what the role of each variable is.  What is 's', and what is
'T'?  And why does date contain the string 'hoo'?

Also, the original code was double-spaced; you don't need to do that.
You're not writing a report that's graded based on filling 80 lines, are
you?  *grin*



> open = close = hi = lo = vol = [1.0]  #is this necessary?

This looks highly suspicious.

In Python, names are just references to things.  That means that all of
these names --- open, close, hi, low, vol --- are just aliases for the
same list thing.  For example:

######
>>> maui = hawaiian = ['pizza']
>>> hawaiian[0] = 'ham and pineapple pizza'
>>> hawaiian
['ham and pineapple pizza']
>>> maui
['ham and pineapple pizza']
######

Notice that because 'maui' and 'hawaiian' are refering to the same list,
when we change the list, we see that reflected here in both 'maui' and
'hawaiian'.



> while s:
>     s = input.readline()
>     if s == '':break
>     s = s[:-2]
>     T[N] = s.split(',')
>     date[N] = T[N][0]
>     open[N] = T[N][1]
>     hi[N] = T[N][2]
>     lo[N] = T[N][3]
>     close[N] = T[N][4]
>     vol[N] = T[N][5]
>     N+=1

Ok, there's a problem here.

One thing you may need to know is that Python's lists do not automatically
expand as we try to enter elements into them.  For example:

######
>>> names = ['knuth', 'morris']
>>> names[2] = 'pratt'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
######

This breaks because the list is defined contain two elements: trying to
assign to names[2] goes out of the list bounds, and that's an error in
Python.  In practice, this "array out of bounds" check is often a Very
Good Thing because index errors can be evidence of a logical program
error.


Anyway, this doesn't answer the problem: how do we add elements to a list?
In Python, we can accumulate elements in a list by append()ing:

######
>>> names = []
>>> names.append('john')
>>> names.append('lisa')
>>> names.append('erik')
>>> names.append('tina')
>>> names
['john', 'lisa', 'erik', 'tina']
######

Does this make sense?




More information about the Tutor mailing list