By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,497 Members | 1,295 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,497 IT Pros & Developers. It's quick & easy.

Need help removing list elements.

P: n/a
This is python 2.4.3 on WinXP under PythonWin.

I have a config file with many blank lines and many other lines that I
don't need.

read the file in, splitlines to make a list, then run a loop that
looks like this:

config_file = open("lines.txt", "rb")
returned_lines = config_file.read().splitlines()

i = len(returned_lines)
for i in range(i):
if returned_lines[i].find("Value") == -1:
if returned_lines[i].find("Name") == -1:
print "read in this useless line ..."
print returned_lines[i]
print "Removing line ..."
returned_lines[i] = ""


This blanks out all the lines I don't want. I did originally try 'del
returned_lines[i]' but I got list index out of range, so I made a loop
to delete the empty elements.

for i in range(i):
if returned_lines[i] == "":

del returned_lines[i]

But this gives me "IndexError: list out of range

After much experimentation and dumping of the list, I have figured out
that it doesn't like removing multiple empty elements in a row. In
other words, if there are 4 empty lines, it will remove one of them,
and seems to behave as though that was one element instead of 3. if I
make i = i - *number of groups of empty elements* it works without an
error, but leaves many empty elements behind.

Obviously I can iterate over it time and again, but that isn't how the
world should work.

Is this something obvious that I am doing wrong, or something more
complicated?

Any help will be gratefully appreciated!

Apr 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
This looks like a job for list comprehensions:
returned_lines= ['Name: John, Value: 12','We don't want this one.','Name: Eric, Value: 24']
[x for x in returned_lines if ('Name' in x and 'Value' in x)]

['Name: John, Value: 12', 'Name: Eric, Value: 24']

List comprehensions are great. If you are not familiar with them, check
out the Python documentation. Once you get started with them, you won't
look back.

Apr 29 '06 #2

P: n/a
Ooops!

Looking at your example a bit closer, change the 'and' in the list
comprehension I posted to 'or', and it should do what you want.

Apr 29 '06 #3

P: n/a
nu*******@gmail.com wrote in
news:11**********************@j73g2000cwa.googlegr oups.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
Apr 29 '06 #4

P: n/a
On 30/04/2006 12:22 AM, Max Erickson wrote:
nu*******@gmail.com wrote in
news:11**********************@j73g2000cwa.googlegr oups.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.


If you are hell-bent on conditionally deleting items from a list in
situ, you need to do it backwards:

for i in xrange(len(alist)-1, -1, -1):
if not_interested(alist[i]):
del alist[i]
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')
Call me crazy, but I wouldn't open the file in BINARY mode :-)
if line != ""]

or just

returned_lines=[line for line in open("lines.txt") if line]


For a modicum of extra effort, the condition "if line.strip()" throws
away lines containing only whitespace.

However I don't see the point of creating a list of lines, then throwing
out only *some* of the uninteresting ones. IMHO the OP might be better
advised to read the file one line at a time, ignoring
blank/empty/comment lines, then *validate* the remainder. Hint: with the
semi-squished-list approach, you can't report the original line number
of any erroneous line without extra effort.

The OP might be even better advised to (read the source of, use) an
existing config file parser module.

Hope some of this helps,
John
Apr 29 '06 #5

P: n/a
Thanks to all those who responded. It has all helped immensely. :-)

I didn't know about list comprehensions before I started.

very warm regards to all.

Apr 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.