Re: Need help removing list elements.
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Need help removing list elements.

From: Max Erickson <maxerickson@gmail.com>
Date: Sat Apr 29 2006 - 16:22:34 CEST

nuffnough@gmail.com wrote in
news:1146316090.540227.278230@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
Received on Mon May 1 00:43:46 2006