Copy List

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Wed Jul 18 07:58:51 EDT 2007


Hello Guys,

 

What's the best way to create a copy of a list? I've seen several method and
I'm not sure what to use. This will be in a class and one method creates a
list which I then want to move to the self scope, like so:

 

__Init__(self):

                Self.myList = []

 

regenerateList(self):

 

                newList = []

 

                for somthing in somthing:

                                newList.append('SomeStuff')

 

                self.Mylist = newList.copy()

 

See, The iteration and rebuilding of the list could be quite slow, during
which time the application could be reading from the self.mylist variable so
i think simply doing this:

 

regenerateList(self):

 

                self.myList = []

 

                for somthing in somthing:

                                self.myList.append('SomeStuff')

 

Might cause a few inconsistencies in the list if it's being read from whilst
I'm appending too it, so I'm guessing that the top example is the best
method, I'm just looking for the best way to copy() that list into the self
scope, should i be using copy(), deepcopy() or simply self.mylist = newlist?

 

Thanks guys,

 

Rob 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070718/86eb8a0a/attachment.html>


More information about the Python-list mailing list