Reading data from file: x(i),y(i) form

Skip Montanaro skip at
Fri Feb 27 23:04:32 CET 2004

    Satish> x=[ ]
    Satish> y=[ ]

    Satish> I get the following error:

    Satish> x[i],y[i]=map(float,line.replace('D','E').split( ))
    Satish> Index Error: list index out of range

That's because at the point you make the assignment neither x nor y have
i'th elements.  Lists are not extended by accessing nonexistent indexes.
You should use x.append and y.append instead (I also prefer list
comprehensions over map()):

    xi,yi= [float(item) for item in line.replace('D','E').split()]


More information about the Python-list mailing list