[Tutor] newbie question about list element manipulation after split()

Visvaldas K. coyote_v2002 at yahoo.com
Thu Oct 16 10:25:01 CEST 2008


I feel very stupid, but I am stuck. I could try some workaround, but I want to understand what I am doing wrong.

The line:

for line in open("parameterfile").readlines( ):
    print line.split()                # trouble line

The "trouble line" works fine in this example. It prints
what I want, something which looks like lists of words:

['CT', 'CT', '268.0', '1.529']...  etc.

However, if I want to manipulate the individual list elements, I run into trouble. While

print line.split()[0]

still works, (it yields CT  - first element), 


print line.split()[1]

gives error:

File "readatoms.py", line 103, in <module>
    print line.split()[1]
IndexError: list index out of range

Could you please tell me what's wrong. (I come from Perl background so Python seems out-of-the-body experience to me).





More information about the Tutor mailing list