Alter list items within loop
Emile van Sebille
emile at fenx.com
Thu Jun 11 15:23:16 EDT 2009
On 6/11/2009 11:54 AM Brendan said...
> Can someone please explain what is happening in the output below?
you delete e[2] before displaying it.
The
> number 3 never gets printed. Does Python make a copy of a list before
> it iterates through it?:
No.
Mods to a list while passing it is generally not a good idea. Sometimes
passing the list backwards works.
Emile
>>>> e = range(1,5)
>>>> for i in e:
> print i
> if i == 2 :
> e.remove(i)
>
>
> 1
> 2
> 4
>>>> e
> [1, 3, 4]
More information about the Python-list
mailing list