Remove empty strings from list

Helvin Lui helvinlui at gmail.com
Mon Sep 14 22:47:53 EDT 2009


Thanks Chris! Thanks for the quick reply. Indeed this is the case! I have
now written out a new list, instead of modifying the list I am iterating
over.
Logged at my blog:
http://learnwithhelvin.blogspot.com/2009/09/python-loop-and-modify-list.html

Regards,
Helvin  =)

On Tue, Sep 15, 2009 at 1:55 PM, Chris Rebert <clp2 at rebertia.com> wrote:

> On Mon, Sep 14, 2009 at 6:49 PM, Helvin <helvinlui at gmail.com> wrote:
> > Hi,
> >
> > Sorry I did not want to bother the group, but I really do not
> > understand this seeming trivial problem.
> > I am reading from a textfile, where each line has 2 values, with
> > spaces before and between the values.
> > I would like to read in these values, but of course, I don't want the
> > whitespaces between them.
> > I have looked at documentation, and how strings and lists work, but I
> > cannot understand the behaviour of the following:
> >                        line = f.readline()
> >                        line = line.lstrip() # take away whitespace at the
> beginning of the
> > readline.
> >                        list = line.split(' ') # split the str line into a
> list
> >
> >                        # the list has empty strings in it, so now,
> > remove these empty strings
> >                        for item in list:
> >                                if item is ' ':
> >                                        print 'discard these: ',item
> >                                        index = list.index(item)
> >                                        del list[index]         # remove
> this item from the list
> >                                else:
> >                                        print 'keep this: ',item
> > The problem is, when my list is :  ['44', '', '', '', '', '',
> > '0.000000000\n']
> > The output is:
> >    len of list:  7
> >    keep this:  44
> >    discard these:
> >    discard these:
> >    discard these:
> > So finally the list is:   ['44', '', '', '0.000000000\n']
> > The code above removes all the empty strings in the middle, all except
> > two. My code seems to miss two of the empty strings.
> >
> > Would you know why this is occuring?
>
> Block quoting from http://effbot.org/zone/python-list.htm
> """
> Note that the for-in statement maintains an internal index, which is
> incremented for each loop iteration. This means that if you modify the
> list you’re looping over, the indexes will get out of sync, and you
> may end up skipping over items, or process the same item multiple
> times.
> """
>
> Thus why your code is skipping over some elements and not removing them.
> Moral: Don't modify a list while iterating over it. Use the loop to
> create a separate, new list from the old one instead.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>



-- 
Helvin

"Though the world may promise me more, I'm just made to be filled with the
Lord."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090915/22e5fc37/attachment.html>


More information about the Python-list mailing list