On Tue, 15 Sep 2009 02:55:13 +0100, Chris Rebert <c...@rebertia.com> wrote:

On Mon, Sep 14, 2009 at 6:49 PM, Helvin <helvin...@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
[snip]

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.

In this case, your life would be improved by using

        l = line.split()

instead of

        l = line.split(' ')

and not getting the empty strings in the first place.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to