[Tutor] For loop breaking string methods

Lie Ryan lie.1296 at gmail.com
Tue Apr 27 11:15:02 CEST 2010

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 have to add to Dave's statement, if you "modify the list's content"
while looping there is no undefined behavior; you get undefined behavior
if you "modify the list's structure".

Operations that modify a list's structure includes insertion, delete,
append, etc; those operations which can modify the len() of list.

Modifying the content, however, is perfectly safe.

However, even when just modifying list's content, I personally still
prefer list/generator comprehension. They're fast and simple.

More information about the Tutor mailing list