Re: [Tutor] iterating over a changing list

2012-10-10 Thread Steven D'Aprano
On 11/10/12 08:49, eryksun wrote: Also, generally avoid mutating a list while iterating over it. listiterator is just incrementing an index, so modifying the size of the list can produce nonsense (e.g. if you remove the current item, the next item will be skipped). Instead, create an empty list

Re: [Tutor] iterating over a changing list

2012-10-10 Thread eryksun
On Wed, Oct 10, 2012 at 3:52 PM, Ed Owens wrote: > > import string Why are you importing "string"? Most string functions one would need are methods of str/unicode. Sometimes "string" is still required, however. > def add_element(items, point): > items = items[:point+1][:] + [['new']] + items

Re: [Tutor] iterating over a changing list

2012-10-10 Thread Dave Angel
On 10/10/2012 03:52 PM, Ed Owens wrote: > I'm trying to iterate over a list of elements, and make changes to the list > in front of the element I'm currently working with. I can update the list, > but the 'for' doesn't see the new element. Here's the code: > > import string > > def add_element(

Re: [Tutor] iterating over a changing list

2012-10-10 Thread Mark Lawrence
On 10/10/2012 20:52, Ed Owens wrote: I'm trying to iterate over a list of elements, and make changes to the list in front of the element I'm currently working with. I can update the list, but the 'for' doesn't see the new element. Here's the code: import string def add_element(items, point)

[Tutor] iterating over a changing list

2012-10-10 Thread Ed Owens
I'm trying to iterate over a list of elements, and make changes to the list in front of the element I'm currently working with. I can update the list, but the 'for' doesn't see the new element. Here's the code: import string def add_element(items, point): items = items[:point+1][:] + [['n