> I have a list that I want to change in a for loop.
Thats nearly always a bad thing to do.
Its like the old cartoon of the guy vcutting off the
branch of the tree while sitting on it. Python can
get very confused.
Either make a copy and change the copy or use
a while loop and an index.
Howe
Very true.
Paul
Kent Johnson wrote:
> Paul D. Eden wrote:
>
>>Lists are mutable, you are right.
>>
>>But the code you gave does not change the list. It changes the variable
>>element which is separate from the list myList.
>>
>>If you want to change the list try something like this:
>>
>>mylis
Paul D. Eden wrote:
> Lists are mutable, you are right.
>
> But the code you gave does not change the list. It changes the variable
> element which is separate from the list myList.
>
> If you want to change the list try something like this:
>
> mylist = [ 'One', ' two', ' three ' ]
Paul D. Kraus wrote:
> I have a list that I want to change in a for loop. It changes in the
> loop but the changes are not persistant outside of the loop.
> I thought lists were mutuable objects
They are.
> so I am a bit confused.
>
> Sample Code
> #!/usr/bin/env python
> """ Testing lists ""
Lists are mutable, you are right.
But the code you gave does not change the list. It changes the variable
element which is separate from the list myList.
If you want to change the list try something like this:
mylist = [ 'One', ' two', ' three ' ]
print mylist
newlist = []
for elemen