Help : IndexError - probably pretty simple

Graham Ashton graz at mindless.com
Thu Oct 18 12:01:57 EDT 2001


In article <fc7bfb77.0110180743.21c90254 at posting.google.com>, "JT"
<x1xx1x at hotmail.com> wrote:

> I've been trying to learn Python and I was working on a function which
> would price options.  But I've run in to an Indexing Error.  This is
> probably pretty simple, but I still haven't figured out what I need to
> change.  It gets hung up at the first "for" loop.  I thought you could
> assign a value to each element of the list individually by placement,
> but maybe I need to use append.

Didn't it work with append()? You're right - append is what you're after:

Python 2.1 (#8, Aug 22 2001, 16:46:14) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> foo = []
>>> foo[0] = 'hello'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
>>> foo       
[]
>>> foo.append('world')
>>> foo
['world']

--
Graham



More information about the Python-list mailing list