473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Is this a refrence issue?

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...

Dec 28 '05 #1
5 1380
KraftDiner wrote:
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
cv*********@mvista.com
Build and Release
MontaVista Software

Dec 28 '05 #2
"KraftDiner" <bo*******@yahoo.com> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 28 '05 #3
On 12/28/05, Carl J. Van Arsdall <cv*********@mvista.com> wrote:
KraftDiner wrote:
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
-carl

--

Carl J. Van Arsdall
cv*********@mvista.com
Build and Release
MontaVista Software

--
http://mail.python.org/mailman/listinfo/python-list

--
James Tanis
jt****@pycoder.org
http://pycoder.org
Dec 28 '05 #4
On Wed, 28 Dec 2005 14:40:45 -0800, Carl J. Van Arsdall wrote:
KraftDiner wrote:
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 = 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.

--
Steven.

Dec 28 '05 #5
KraftDiner wrote:
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
sc***********@acm.org
Dec 29 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Phillips | last post by:
Hello everyone, I have read the section on templates in The C++ Programming Language 3rd edition 3 times over the last 4 days. Still I am stumped. I'll explain a bit about what I am doing...
6
by: A.M | last post by:
Hi, Ho can I send a refrence type by value as a method parameter. Thanks, Alan
2
by: | last post by:
i have a form name Form1 i want to create a copy of Form2 which know th refrence of Form1. example: private: System::Void button1_Click(System::Object * sender, System::EventArgs * e) { Form2...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
0
by: Gary | last post by:
I'm using VB.net in Visual Studio .net 2005. I've created a web service and have it hosted on a private server (Windows 2003 - IIS 6.0). I can add the web service as a Web Refrrence, but it...
3
by: preitymathur0422 | last post by:
I m working on a MDI application. Here on the parent form I had two variables declared as public int curUserID = 0, ViewUserID = 1; the form is inherited by the "Form" class. I m using these...
1
by: bhavinp | last post by:
hi, following my code snippet void ObAuthUser::SetInstanceFromToken(ObKeyValMap const & rKeyValMap) { char const * pAuthId = rKeyValMap.GetVal(kAuthIdKey);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.