[Tutor] For loop breaking string methods

Dave Angel 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[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] = some_func(value)
>
> Is this the standard method?
>
> [1]: 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.

DaveA



More information about the Tutor mailing list