[Tutor] For loop breaking string methods
davea at ieee.org
Tue Apr 27 04:19:48 CEST 2010
C M Caine wrote:
> Thank you for the clarification, bob.
> For any future readers of this thread I include this link 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 = some_func(value)
> Is this the standard method?
> : http://effbot.org/zone/python-list.htm
> Colin Caine
Almost. You should have said
L[index] = some_func(value)
The way you had it, it would only replace the zeroth item of the list.
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.
More information about the Tutor