[Tutor] append index out of range

Dave Angel d at davea.name
Thu Jan 12 15:56:22 CET 2012


On 01/12/2012 09:38 AM, lina wrote:
> Hi,
>
> there is a file
>
> $ cat atom-pair_9.out | wc -l
> 75426
>
> there is 75426 lines there,
>
>      results=[]
>      unique={}
>      for line in open(tobetranslatedfile,"r"):
>          tobetranslatedparts=line.strip().split()
>          results.append(dictionary[tobetranslatedparts[2]])
>
> it complains
>
>      results.append(dictionary[tobetranslatedparts[2]])
> IndexError: list index out of range
>
>
> is it really too large this file?
>
This problem has nothing to do with the size of the file, nor with append().

You've got a lot going on in that line.  To solve an error message on a 
complex line, decompose it, either with extra prints, or with a literal 
decomposition into separate variables.

Personally, I'd start with a print of the tobetranslatedparts variable.  
it is a list, after all, and the error message is complaining about a 
list index.

Then, when it has printed out many lists, figure out what's different 
about the last one, the one that triggered the error.

Once you've figured out what's wrong with that line, figure out how to 
deal with it.  Does it mean the file is invalid?  Or does it mean your 
loop has to be somewhat more complex?

-- 

DaveA



More information about the Tutor mailing list