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 element in mylist: element = element.strip() newlist.append(element) print "<>" + element + "<>" print newlist OR mylist = [ 'One ', ' two', ' three ' ] print mylist mylist = [element.strip() for element in mylist] for element in mylist: print "<>" + element + "<>" print mylist Paul 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 so I am a bit confused. > > Sample Code.... > #!/usr/bin/env python > """ Testing lists """ > > mylist = [ 'One ', ' two', ' three ' ] > print mylist > for element in mylist: > element = element.strip() > print "<>" + element + "<>" > print mylist > > > Sample Output.... > ['One ', ' two', ' three '] > <>One<> > <>two<> > <>three<> > ['One ', ' two', ' three '] > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor