Re: Need help removing list elements.

2006-04-29 Thread nuffnough
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. -- http://mail.python.org/mailman/listinfo/python-list

Re: Need help removing list elements.

2006-04-29 Thread John Machin
On 30/04/2006 12:22 AM, Max Erickson wrote: > [EMAIL PROTECTED] wrote in > news:[EMAIL PROTECTED]: > >> 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

Re: Need help removing list elements.

2006-04-29 Thread Max Erickson
[EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: > 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

Re: Need help removing list elements.

2006-04-29 Thread jwelby
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. -- http://mail.python.org/mailman/listinfo/python-list

Re: Need help removing list elements.

2006-04-29 Thread jwelby
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. I

Need help removing list elements.

2006-04-29 Thread nuffnough
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().splitl