[Tutor] Removing lines in string-table

Olli Rajala olli.rajala at gmail.com
Tue May 17 18:19:30 CEST 2005


My code:
> > for line in fileNames:
> >     if line[-10:] == '_thumb.jpg':
> >         fileNames.remove(line)

Chris wrote:
> The above will not work if two successive lines contain the target
> text. When you remove the one, its neighbor "slides over" to take the
> place of the one removed and then when you proceed to the "next" line
> you are actually skipping the one that slid over.  

Oh, yeah, that's right. I just didn't notice it... Thanks for
correcting me! Actually it wouldn't have mattered (I think) because
the list contains x.jpg and x_thumb.jpg which have been collected by
os.listdir(). At least I suppose that it would be like [x.jpg,
x_thumb.jpg, y.jpg, y_thumb.jpg] or am I completely wrong? But thanks
for good suggestions, I replaced my code with the list comprehension
method and it works now. Thanks!

Yours, 
-- 
Olli Rajala <><
Tampere, Finland
http://www.students.tut.fi/~rajala37/

"In theory, Theory and Practice should be
the same. But in practice, they aren't."
- Murphy's Proverbs


More information about the Tutor mailing list