473,574 Members | 3,131 Online

# copy list, which way is best? /style

Hi,

There are three ways to (shallow)copy a list l I'm aware of:
l2=list(l)
l2=l[:]
l2.copy.copy(l)

Are there any differences? Are there more (reasonable) ways?
I think the first is the most pythonic, second looks more like this other
p-language and third requires an import, so I would prefer the first.
Do you agree?

Andreas
Jul 18 '05 #1
2 35765
Andreas Kuntzagk wrote:
Hi,

There are three ways to (shallow)copy a list l I'm aware of:
l2=list(l)
l2=l[:]
l2.copy.copy(l)

Are there any differences? Are there more (reasonable) ways?
I think the first is the most pythonic, second looks more like this other
p-language and third requires an import, so I would prefer the first.
Do you agree?

Andreas

The way I'd do it is

from copy import copy
l2 = copy(l1)

or

from copy import deepcopy
l2 = deepcopy(l1)

I don't know what the difference is, if any, but I think this way is more

HTH,
Andrew Wilkinson

--
Study + Hard Work + Loud Profanity = Good Code
Jul 18 '05 #2
"Andreas Kuntzagk" <an************ **@mdc-berlin.de> wrote in
news:be******** ****@fu-berlin.de:
There are three ways to (shallow)copy a list l I'm aware of:
l2=list(l)
l2=l[:]
l2.copy.copy(l)
Are there any differences? Are there more (reasonable) ways?
I think the first is the most pythonic, second looks more like this other
p-language and third requires an import, so I would prefer the first.
Do you agree?

They all do slightly different things.
I think this is a fairly accurate description of what each of these does:
l2 = list(l) This will copy any iterable object and will produce a new, distinct list as
a result.
l2=l[:] This will copy a sequence, and will return an object of the same type as
the original. If the original is immutable, then it may simply return the
original object and not bother with making a copy.
l2=copy.copy(l) This will copy any object whether or not it is a sequence, but it may still
return the original object for immutables.
l2=copy.deepcop y(l)

This will make a deep copy of an object. It only returns the original
object for immutables if any objects they contain are also immutable
(including their contents).

--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #3

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

### Similar topics

 19 4647 by: Claudio Grondi | last post by: I would like to save time copying the same file (>6 GByte) to various different target storage media connected to the system via USB. Is there a (Python or other) tool able to help me to do this, so that I don't need to copy the source file first to the first media, then to the second, etc.? Claudio 5 2438 by: sam | last post by: Hi, The following code produced a core-dump: PropertyParser::PropertyParser(list &p_list) { l_conf_data.clear(); cout << "beginning copy.. size: " << p_list.size() << endl; copy(p_list.begin(), p_list.end(), l_conf_data.begin()); cout << "finished copy..." << endl; } 3 16762 by: Douwe | last post by: I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version which uses the "..." as the last parameter how can I pass them to the orignal printf ? void myprintf(char *txt, ...) printf(txt, ???????); } 4 6200 by: Sin Jeong-hun | last post by: Lista=param; Listb=a; If I change b, then a is get changed. I want another copy of a, that is completely independent of a. I used double-nested for loop to copy each element manually. Is there any more efficent way to do that? Something like, Listb=CreateClone(a); Thanks. 13 1829 by: Mounir | last post by: Hi, Assume that right and left are multiple select elements. It's about the following line : right.options=left.options; It copies the content of left.options into right.options, but *removes* the content of the first one ! 1 2794 by: xqxu.pzhou | last post by: I wrote a simple allocator "myAlloc" under the g++ 3.2.3. When it is used by Vector, it works well. But when it is used by List, the codes have errors when compling. the error message is: "no matching function for call to myAlloc Then I added the following copy constructor to "myAlloc", now it works well. "template... 5 6610 by: jgscott | last post by: I've been trawling around for an answer to this question and thought I'd try here. I have a class Graph, which has a std::list