473,769 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4675
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
2451
by: sam | last post by:
Hi, The following code produced a core-dump: PropertyParser::PropertyParser(list<HashMap> &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
16778
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:
List<List<T>a=param; List<List<T>b=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, List<List<T>b=CreateClone(a); Thanks.
13
1847
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
2804
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<std::_List_node<int> Then I added the following copy constructor to "myAlloc", now it works well. "template <typename U> myAlloc(const myAlloc<U>&) {}"
5
6629
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<Nodeas a class member. Node it itself a class that makes extensive use of pointers to represent various forms of data in the nodes of graphs, along with pointers to neighboring nodes. This necessitates a deep copy that iterators over the list. My copy constructor looks like: ...
4
987
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another copy which in itself needs a copy constructor. However i was wondering why can't the existing object be passed as a pointer instead of a reference to the copy constructor which creates a new object.
0
1793
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is reasonable since generators require iteration and stop iteration and what not.
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.