473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Need help removing list elements.

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
5 1920
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tino | last post by:
I have a std::vector<int> which, after some initialization, has a fixed number of elements...after initialization I must do the following repeatedly: I remove an element which could be anywhere in...
3
by: pr10n | last post by:
Hello, all! I'm working on a personal blog and I'm trying to make it's markup more semantic, using the right elements, removing needless div's and so on. I was wondering if someone would be kind...
3
by: Amit | last post by:
Hi, I have a list of integers. At each iteration, I remove some element from it and then insert new elements in it. The order of elements is not important. So I guess I could use a vector also for...
2
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
7
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could tell me if there is an efficient way to do the following. Say I have a list(or vector) A and a list B, I want to remove any elements in B, that are also...
2
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could tell me if there is an efficient way to do the following. Say I have a list(or vector) A and a list B, I want to remove any elements in B, that are also...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
3
m6s
by: m6s | last post by:
Hello to all, I am having trouble removing an item from list, which the item is a list by itself. <code> for self.line in self.filtered: if self.appid: if self.line <> self.appid: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.