Is this a refrence issue?

K

KraftDiner

I understand that everything in python is a refrence....

I have a small problem..

I have a list and want to make a copy of it and add an element to the
end of the new list,
but keep the original intact....

so:
tmp = myList
tmp.append(something)
print tmp, myList

should be different...
 
C

Carl J. Van Arsdall

KraftDiner said:
I understand that everything in python is a refrence....

I have a small problem..

I have a list and want to make a copy of it and add an element to the
end of the new list,
but keep the original intact....

so:
tmp = myList

tmp = myList is a shallow copy


tmp.append(something)
print tmp, myList

should be different...
Therefore it shouldn't be different.

What you could do is make a second list
tmp = []

And then use the extend method:

tmp.extend(myList)
tmp.append(someEntry)

This would give you what you want, and there's more than one way to do this.

-carl

--

Carl J. Van Arsdall
(e-mail address removed)
Build and Release
MontaVista Software
 
M

Mike Meyer

KraftDiner said:
I understand that everything in python is a refrence....
Correct.

I have a small problem..

Maybenot so small.
I have a list and want to make a copy of it and add an element to the
end of the new list,
but keep the original intact....

so:
tmp = myList
tmp.append(something)
print tmp, myList

should be different...

They won't be, because the assignment statement just binds the name on
the left to the value on the right, meaning they'll both point to the
same object. To get a copy, you have to make a copy. The easy way is:

tmp = list(myList)

However, this won't coppy the things *in* myList. If you want that you
want the copy module's deepcopy function.

<mike
 
J

James Tanis

KraftDiner said:
I understand that everything in python is a refrence....

I have a small problem..

I have a list and want to make a copy of it and add an element to the
end of the new list,
but keep the original intact....

so:
tmp = myList

tmp = myList is a shallow copy


tmp.append(something)
print tmp, myList

should be different...
Therefore it shouldn't be different.

What you could do is make a second list
tmp = []

And then use the extend method:

tmp.extend(myList)
tmp.append(someEntry)

This would give you what you want, and there's more than one way to do this.

Such as from the python docs..

import copy

x = copy.copy(y) # make a shallow copy of y
x = copy.deepcopy(y) # make a deep copy of y
 
S

Steven D'Aprano

tmp = myList is a shallow copy


tmp = myList *is not a copy at all*.

The *names* "tmp" and "myList" both are bound to the *same* object. They
are two names for the same object.

tmp = myList[:] is a shallow copy of myList.

tmp = copy.deepcopy(myList) makes a copy of myList, *and* copies of
everything inside myList.
 
S

Scott David Daniels

KraftDiner said:
I have a list and want to make a copy of it and add an element
to the end of the new list, but keep the original intact....

Nobody has mentioned the obvious yet:

tmp = myList + [something]

--Scott David Daniels
(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top