[Tutor] Iterating through a list of strings

Thomas C. Hicks paradox at pobox.com
Mon May 3 07:16:17 CEST 2010


I am using Python 2.6.4 in Ubuntu.  Since I use Ubuntu (with its every
6 months updates) and want to learn Python I have been working on a
post-install script that would get my Ubuntu system up and running with
my favorite packages quickly.  Basically the script reads a text file,
processes the lines in the file and then does an apt-get for each line
that is a package name.  The text file looks like this:

%Comment introducing the next block of packages
%Below are the packages for using Chinese on the system
%Third line of comment because I am a verbose guy!
ibus-pinyin
ibus-table-wubi
language-pack-zh-hans

etc.

I read the lines of the file into a list for processing.  To strip
out the comments lines I am using something like this:

for x in lines:
    if x.startswith('%'):
        lines.remove(x)

This works great for all incidents of comments that are only one
line.  Sometimes I have blocks of comments that are more than one
line and find that the odd numbered lines are stripped from the list
but not the even numbered lines (i.e in the above block the line
"%Below are the ..." line would not be stripped out of the
list).

Obviously there is something I don't understand about processing
the items in the list and using the string function x.startswith() and
the list function list.remove(). Interestingly if I put in "print x"
in place of the lines.remove(x) line I get all the comment lines
printed.

Can anyone point me in the right direction?  

thomas


More information about the Tutor mailing list