decouple copy of a list

Discussion in 'Python' started by Dirk Nachbar, Dec 10, 2010.

  1. Dirk Nachbar

    Dirk Nachbar Guest

    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
     
    Dirk Nachbar, Dec 10, 2010
    #1
    1. Advertising

  2. 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
     
    Kushal Kumaran, Dec 10, 2010
    #2
    1. Advertising

  3. Dirk Nachbar

    Daniel Urban Guest

    b = list(a)

    or

    b = a[:]
     
    Daniel Urban, Dec 10, 2010
    #3
  4. 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
     
    Wolfgang Rohdewald, Dec 10, 2010
    #4
  5. 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
     
    Jean-Michel Pichavant, Dec 10, 2010
    #5
  6. Dirk Nachbar

    Dirk Nachbar Guest

    On Dec 10, 1:56 pm, Wolfgang Rohdewald <> 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.
     
    Dirk Nachbar, Dec 10, 2010
    #6
  7. 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
     
    Wolfgang Rohdewald, Dec 10, 2010
    #7
  8. Dirk Nachbar

    cassiope Guest

    On Dec 10, 6:06 am, Jean-Michel Pichavant <>
    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...
     
    cassiope, Dec 10, 2010
    #8
  9. Dirk Nachbar

    nn Guest

    On Dec 10, 8:48 am, 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


    Not knowing the particulars,
    you may have to use:

    import copy
    b=copy.deepcopy(a)
     
    nn, Dec 10, 2010
    #9
  10. 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.

    ....

    :D 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
     
    Jean-Michel Pichavant, Dec 10, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    4
    Views:
    1,089
    John Saunders
    Jan 12, 2005
  2. Alex
    Replies:
    2
    Views:
    1,237
  3. Replies:
    26
    Views:
    2,121
    Roland Pibinger
    Sep 1, 2006
  4. Ethan Kennerly
    Replies:
    8
    Views:
    321
    Bruno Desthuilliers
    Jun 20, 2007
  5. epaetz

    decouple parent window from children

    epaetz, Jul 19, 2006, in forum: Javascript
    Replies:
    2
    Views:
    104
    epaetz
    Jul 24, 2006
Loading...

Share This Page