[Tutor] For loop breaking string methods

C M Caine cmcaine at googlemail.com
Mon Apr 26 21:38:17 CEST 2010


Why does this not work:
>>> L = [' foo ','bar ']
>>> for i in L:
    i = i.strip()


>>> L
[' foo ', 'bar ']
>>> # note the leading whitespace that has not been removed.

But this does:
>>> L = [i.strip() for i in L]
>>> L
['foo', 'bar']

What other strange behaviour should I expect from for loops?

Thanks,
Colin Caine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100426/1d8fd1e5/attachment.html>


More information about the Tutor mailing list