List problems in C code ported to Python

wittempj at hotmail.com wittempj at hotmail.com
Mon Jan 17 12:10:14 EST 2005


>>> l = []
>>> for i in range(2):
for j in range(2):
l[i][j] = 'x'



Traceback (most recent call last):
File "<pyshell#7>", line 3, in -toplevel-
l[i][j] = 'x'
IndexError: list index out of range

So you still have to dimension the list before you can use it , eg like
>l = []
>for i in range(2):
>    l.append([])
>    for j in range(2):
>        l[i].append(j)

then you do not get the indexerror, but I feel a class is what you need
here to make life easier and the program more readable




More information about the Python-list mailing list