Re: [Tutor] For loop breaking string methods

2010-04-27 Thread Lie Ryan
On 04/27/10 12:19, Dave Angel wrote: Note also that if you insert or delete from the list while you're looping, you can get undefined results. That's one reason it's common to build a new loop, and just assign it back when done. Example would be the list comprehension you showed earlier. I

Re: [Tutor] For loop breaking string methods

2010-04-27 Thread bob gailer
On 4/26/2010 10:19 PM, Dave Angel wrote: Note also that if you insert or delete from the list while you're looping, you can get undefined results. The results are not undefined. They m,ight be unexpected, but that is due to an invalid assumption. The behavior is explained in section 7.3

[Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
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

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread bob gailer
On 4/26/2010 3:38 PM, C M Caine wrote: Why does this not work: By work you mean do what I want it to do. L = [' foo ','bar '] for i in L: i = i.strip() This creates a new local variable named i. It does not affect L. This has nothing to do with loops nor is it strange behavior - it

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
Thank you for the clarification, bob. For any future readers of this thread I include this link[1] to effbot's guide on lists, which I probably should have already read. My intention now is to modify list contents in the following fashion: for index, value in enumerate(L): L[0] =

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Sander Sweers
On 26 April 2010 21:38, C M Caine cmca...@googlemail.com wrote: Why does this not work: L = [' foo ','bar '] for i in L:     i = i.strip() str.strip() _returns_ a *new* string and leaves the original string alone. The reason being that string are immutable so can not be changed. s1 = ' foo

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Alan Gauld
C M Caine cmca...@googlemail.com wrote My intention now is to modify list contents in the following fashion: for index, value in enumerate(L): L[0] = some_func(value) I think you mean: L[index] = some_func(value) Is this the standard method? Or use a List copmprehension. L =

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
What other strange behaviour should I expect from for loops? You should read up on immutable data types like strings and tuples. Start with [1]. Greets Sander [1] http://docs.python.org/reference/datamodel.html Thank you kindly for your reply, I'll be sure to read up on it. Colin

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread C M Caine
On 26 April 2010 23:45, Alan Gauld alan.ga...@btinternet.com wrote: C M Caine cmca...@googlemail.com wrote My intention now is to modify list contents in the following fashion: for index, value in enumerate(L):   L[0] = some_func(value) I think you mean:     L[index] = some_func(value)

Re: [Tutor] For loop breaking string methods

2010-04-26 Thread Dave Angel
C M Caine wrote: Thank you for the clarification, bob. For any future readers of this thread I include this link[1] to effbot's guide on lists, which I probably should have already read. My intention now is to modify list contents in the following fashion: for index, value in enumerate(L):