Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > decouple copy of a list

Reply
Thread Tools

decouple copy of a list

 
 
Dirk Nachbar
Guest
Posts: n/a
 
      12-10-2010
I want to take a copy of a list a

b=a

and then do things with b which don't affect a.

How can I do this?

Dirk
 
Reply With Quote
 
 
 
 
Kushal Kumaran
Guest
Posts: n/a
 
      12-10-2010
On Fri, Dec 10, 2010 at 7:18 PM, Dirk Nachbar <> wrote:
> I want to take a copy of a list a
>
> b=a
>
> and then do things with b which don't affect a.
>
> How can I do this?
>


b = a[:] will create a copy of the list. If the elements of the list
are references to mutable objects (objects of your own classes, for
example), you might take a look at copy.deepcopy.

--
regards,
kushal
 
Reply With Quote
 
 
 
 
Daniel Urban
Guest
Posts: n/a
 
      12-10-2010
b = list(a)

or

b = a[:]
 
Reply With Quote
 
Wolfgang Rohdewald
Guest
Posts: n/a
 
      12-10-2010
On Freitag 10 Dezember 2010, Dirk Nachbar wrote:
> I want to take a copy of a list a
>
> b=a
>
> and then do things with b which don't affect a.
>
> How can I do this?
>
> Dirk


b=a[:]


--
Wolfgang
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      12-10-2010
Dirk Nachbar wrote:
> I want to take a copy of a list a
>
> b=a
>
> and then do things with b which don't affect a.
>
> How can I do this?
>
> Dirk
>

In [1]: a = [1,2,3]

In [2]: b = a[:]

In [3]: b[0] = 5

In [4]: a
Out[4]: [1, 2, 3]

In [5]: b
Out[5]: [5, 2, 3]


Alternatively, you can write

import copy
a = [1,2,3]
b = a.copy()

if the list a contains mutable objects, use copy.deepcopy
(http://docs.python.org/library/copy.html)


JM
 
Reply With Quote
 
Dirk Nachbar
Guest
Posts: n/a
 
      12-10-2010
On Dec 10, 1:56*pm, Wolfgang Rohdewald <wolfg...@rohdewald.de> wrote:
> On Freitag 10 Dezember 2010, Dirk Nachbar wrote:
>
> > I want to take a copy of a list a

>
> > b=a

>
> > and then do things with b which don't affect a.

>
> > How can I do this?

>
> > Dirk

>
> b=a[:]
>
> --
> Wolfgang


I did that but then some things I do with b happen to a as well.
 
Reply With Quote
 
Wolfgang Rohdewald
Guest
Posts: n/a
 
      12-10-2010
On Freitag 10 Dezember 2010, Dirk Nachbar wrote:
> > b=a[:]
> >
> > --
> > Wolfgang

>
> I did that but then some things I do with b happen to a as
> well.


as others said, this is no deep copy. So if you do something
to an element in b, and if the same element is in a, both
are changed as they are still the same objects:

>>> x,y=5,6
>>> a=[x,y]
>>> b=a[:]
>>> id(a),id(b)

(140695481867368, 140695481867512)
>>> id(a[0]),id(b[0])

(33530584, 33530584)
>>> a=b
>>> id(a),id(b)

(140695481867512, 140695481867512)


--
Wolfgang
 
Reply With Quote
 
cassiope
Guest
Posts: n/a
 
      12-10-2010
On Dec 10, 6:06*am, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
> Dirk Nachbar wrote:
> > I want to take a copy of a list a

>
> > b=a

>
> > and then do things with b which don't affect a.

>
> > How can I do this?

>
> > Dirk

>
> In [1]: a = [1,2,3]
>
> In [2]: b = a[:]
>
> In [3]: b[0] = 5
>
> In [4]: a
> Out[4]: [1, 2, 3]
>
> In [5]: b
> Out[5]: [5, 2, 3]
>
> Alternatively, you can write
>
> import copy
> a = [1,2,3]
> b = a.copy()
>
> if the list a contains mutable objects, use copy.deepcopy
> (http://docs.python.org/library/copy.html)
>
> JM


I'm not a pyguru, but... you didn't use copy quite right.
Try instead: b= copy.copy(a)

The other issue that the original person has noticed is that
a list may include a reference to something. When a list is
copied - if the reference is copied (not "deepcopied"], changes
to the referred object will be visible in both lists, even if
they are different lists.

For more information, refer to the docs in the <copy> module.

HTH...
 
Reply With Quote
 
nn
Guest
Posts: n/a
 
      12-10-2010
On Dec 10, 8:48*am, Dirk Nachbar <dirk...@gmail.com> wrote:
> I want to take a copy of a list a
>
> b=a
>
> and then do things with b which don't affect a.
>
> How can I do this?
>
> Dirk


Not knowing the particulars,
you may have to use:

import copy
b=copy.deepcopy(a)
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      12-10-2010
cassiope wrote:
>> Alternatively, you can write
>>
>> import copy
>> a = [1,2,3]
>> b = a.copy()
>>
>>
>> JM
>>

>
> I'm not a pyguru, but... you didn't use copy quite right.
> Try instead: b= copy.copy(a)
>
>

You're right, you're not a python guru so don't even try to contradict
me ever again.

....

of course I did it completly wrong. I don't know what happened in my
brain at that time, maybe nothing and that's the point.

JM

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy List of Files to a Text List ? Martin Knight Computer Support 5 09-09-2011 11:51 PM
Does altering a private member decouple the property's value? Ethan Kennerly Python 8 06-20-2007 12:37 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
decouple parent window from children epaetz Javascript 2 07-24-2006 03:09 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57