deleteing item from a copy of a list

Fredrik Lundh fredrik at
Tue Nov 14 10:18:15 CET 2006

timmy wrote:

> i make a copy of a list, and delete an item from it and it deletes it 
> from the orginal as well, what the hell is going on?!?!?!
> #create the staff copy of the roster
> 	Roster2 = []
> 	for ShiftLine in Roster:
> 		#delete phone number from staff copy
> 		Roster2.append(ShiftLine)
> 		del Roster2[len(Roster2)-1][1]
> Roster2 should have nothing to do with Roster, right??? doing a print of 
> both lists confirms that the phone number has been removed from both

you're not removing something from Roster2, you're removing something 
from the last item in Roster2.  the append methods adds a reference to 
an object to a list; it doesn't copy the object.


More information about the Python-list mailing list