Need help removing list elements.
Max Erickson
maxerickson at gmail.com
Sat Apr 29 10:22:34 EDT 2006
nuffnough at gmail.com wrote in
news:1146316090.540227.278230 at j73g2000cwa.googlegroups.com:
> But this gives me "IndexError: list out of range
You are making the list shorter as you are iterating. By the time your
index is at the end of the original list, it isn't that long any more.
Creating a new list and appending the elements you want to keep avoids
the problem. Or you can just use a list comprehension(untested):
returned_lines=[line for line in open("lines.txt", 'rb')
if line != ""]
or just
returned_lines=[line for line in open("lines.txt") if line]
max
More information about the Python-list
mailing list