Need help removing list elements.

Discussion in 'Python' started by nuffnough@gmail.com, Apr 29, 2006.

  1. Guest

    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.find("Value") == -1:
    if returned_lines.find("Name") == -1:
    print "read in this useless line ..."
    print returned_lines
    print "Removing line ..."
    returned_lines = ""




    This blanks out all the lines I don't want. I did originally try 'del
    returned_lines' 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 == "":

    del returned_lines



    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, 2006
    #1
    1. Advertising

  2. jwelby Guest

    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.
    jwelby, Apr 29, 2006
    #2
    1. Advertising

  3. jwelby Guest

    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.
    jwelby, Apr 29, 2006
    #3
  4. Max Erickson Guest

    wrote in
    news::

    > 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
    Max Erickson, Apr 29, 2006
    #4
  5. John Machin Guest

    On 30/04/2006 12:22 AM, Max Erickson wrote:
    > wrote in
    > news::
    >
    >> 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):
    del alist

    > 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
    John Machin, Apr 29, 2006
    #5
  6. Guest

    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, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Helge Richter

    Removing Elements from a Vector

    Helge Richter, Feb 27, 2004, in forum: Java
    Replies:
    5
    Views:
    6,411
    Andrew Hobbs
    Feb 28, 2004
  2. Chris  Chiasson
    Replies:
    6
    Views:
    612
    Richard Tobin
    Nov 14, 2006
  3. Adam Hartshorne
    Replies:
    7
    Views:
    356
    Pete Becker
    Jan 27, 2006
  4. Adam Hartshorne
    Replies:
    2
    Views:
    372
    Nitin Motgi
    Jan 27, 2006
  5. Debajit Adhikary
    Replies:
    17
    Views:
    680
    Debajit Adhikary
    Oct 18, 2007
Loading...

Share This Page