deleteing item from a copy of a list

T

timmy

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
 
F

Fredrik Lundh

timmy said:
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.

</F>
 
P

Peter Otten

timmy said:
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 seem to have a list of lists and are making a new outer list. The lists
inside that outer list are not copied. You can copy them by calling
list(item) or copy.copy(item):
.... ["Tim", "12345", "some more"],
.... ["Jack", "54321", "whatever"],
.... ]
roster2 = []
for item in roster:
.... item = list(item)
.... del item[1]
.... roster2.append(item)
....
roster [['Tim', '12345', 'some more'], ['Jack', '54321', 'whatever']]
roster2
[['Tim', 'some more'], ['Jack', 'whatever']]

Peter
 
M

Mikael Olofsson

timmy said:
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

First of all, you could have said

del Roster2[-1][1]

since negative indices count backwards from the end of the list. This
has nothing to do with your problem, though.

Fredrik has already given you a correct, somewhat condensed, answer. Let
me elaborate. I guess that Roster is a list of lists. Then actually, it
is a list of references to lists (really a reference to a list of
references to lists). Your for-loop makes Roster2 a shallow copy of
Roster, i.e. Roster2 is a new list with references to the _same_
sublists as in Roster. So, Roster2[-1] references the same object as
Roster[-1], not a copy of it. To get copies, you could change your
..append-line to

Roster2.append(ShiftLine[:])

which gives you shallow copies of the sublists. If there are mutables in
the sublists, you may still get into trouble. In that case, maybe you
should take a look at copy.deepcopy.

HTH
/MiO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top