[Tutor] list iteration [iteration and mutation?]

Chad Crabtree flaxeater at yahoo.com
Mon May 3 10:34:17 EDT 2004


> If that's case, it might be nicer to explicitely
> rewrite:
> 
>     for item in itemslist[:]: ...
> 
> as two statements:
> 
>     copiedList = itemslist[:]
>     for item in copiedList: ...
> 
> The intent, to iterate across the copy of a list, is
> odd enough that it
> probably needs explanation, either with a comment,
> or with the
> two-statement breakup that we have above, so that we
> make that intent very
> clear to a human reader.
> 
> 
> Good luck to you!

  I wanted to chime in a moment about this.  I
recently ran in to this very bug where I needed to
remove items from a list.  I was making a GUI two list
box's you click on a button to send items from one
listbox to the other and backand forth.   Naturally
when deleted items  I got an out of index error.  What
I did was built a list of index numbers then iterated
over them from highest to lowest using .reverse().  

		self.buildlist=list(self.origin.GetSelections())
		for v in self.buildlist:
			self.destlist.append(self.emotionlist[v])
		self.buildlist.reverse()
		for v in self.buildlist:
			del self.emotionlist[v]

Nothing fantastic.



	
		
__________________________________
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs  
http://hotjobs.sweepstakes.yahoo.com/careermakeover 



More information about the Tutor mailing list