Is this a refrence issue?

Discussion in 'Python' started by KraftDiner, Dec 28, 2005.

  1. KraftDiner

    KraftDiner Guest

    I understand that everything in python is a refrence....

    I have a small problem..

    I have a list and want to make a copy of it and add an element to the
    end of the new list,
    but keep the original intact....

    so:
    tmp = myList
    tmp.append(something)
    print tmp, myList

    should be different...
    KraftDiner, Dec 28, 2005
    #1
    1. Advertising

  2. KraftDiner wrote:
    > I understand that everything in python is a refrence....
    >
    > I have a small problem..
    >
    > I have a list and want to make a copy of it and add an element to the
    > end of the new list,
    > but keep the original intact....
    >
    > so:
    > tmp = myList
    >


    tmp = myList is a shallow copy



    > tmp.append(something)
    > print tmp, myList
    >
    > should be different...
    >

    Therefore it shouldn't be different.

    What you could do is make a second list
    tmp = []

    And then use the extend method:

    tmp.extend(myList)
    tmp.append(someEntry)

    This would give you what you want, and there's more than one way to do this.

    -carl

    --

    Carl J. Van Arsdall

    Build and Release
    MontaVista Software
    Carl J. Van Arsdall, Dec 28, 2005
    #2
    1. Advertising

  3. KraftDiner

    Mike Meyer Guest

    "KraftDiner" <> writes:
    > I understand that everything in python is a refrence....


    Correct.

    > I have a small problem..


    Maybenot so small.

    > I have a list and want to make a copy of it and add an element to the
    > end of the new list,
    > but keep the original intact....
    >
    > so:
    > tmp = myList
    > tmp.append(something)
    > print tmp, myList
    >
    > should be different...


    They won't be, because the assignment statement just binds the name on
    the left to the value on the right, meaning they'll both point to the
    same object. To get a copy, you have to make a copy. The easy way is:

    tmp = list(myList)

    However, this won't coppy the things *in* myList. If you want that you
    want the copy module's deepcopy function.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Dec 28, 2005
    #3
  4. KraftDiner

    James Tanis Guest

    On 12/28/05, Carl J. Van Arsdall <> wrote:
    > KraftDiner wrote:
    > > I understand that everything in python is a refrence....
    > >
    > > I have a small problem..
    > >
    > > I have a list and want to make a copy of it and add an element to the
    > > end of the new list,
    > > but keep the original intact....
    > >
    > > so:
    > > tmp = myList
    > >

    >
    > tmp = myList is a shallow copy
    >
    >
    >
    > > tmp.append(something)
    > > print tmp, myList
    > >
    > > should be different...
    > >

    > Therefore it shouldn't be different.
    >
    > What you could do is make a second list
    > tmp = []
    >
    > And then use the extend method:
    >
    > tmp.extend(myList)
    > tmp.append(someEntry)
    >
    > This would give you what you want, and there's more than one way to do this.
    >


    Such as from the python docs..

    import copy

    x = copy.copy(y) # make a shallow copy of y
    x = copy.deepcopy(y) # make a deep copy of y

    > -carl
    >
    > --
    >
    > Carl J. Van Arsdall
    >
    > Build and Release
    > MontaVista Software
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    James Tanis

    http://pycoder.org
    James Tanis, Dec 28, 2005
    #4
  5. On Wed, 28 Dec 2005 14:40:45 -0800, Carl J. Van Arsdall wrote:

    > KraftDiner wrote:
    >> I understand that everything in python is a refrence....

    >
    >> I have a small problem..
    >>
    >> I have a list and want to make a copy of it and add an element to the
    >> end of the new list,
    >> but keep the original intact....
    >>
    >> so:
    >> tmp = myList
    >>

    >
    > tmp = myList is a shallow copy



    tmp = myList *is not a copy at all*.

    The *names* "tmp" and "myList" both are bound to the *same* object. They
    are two names for the same object.

    tmp = myList[:] is a shallow copy of myList.

    tmp = copy.deepcopy(myList) makes a copy of myList, *and* copies of
    everything inside myList.



    --
    Steven.
    Steven D'Aprano, Dec 28, 2005
    #5
  6. KraftDiner wrote:
    > I have a list and want to make a copy of it and add an element
    > to the end of the new list, but keep the original intact....


    Nobody has mentioned the obvious yet:

    tmp = myList + [something]

    --Scott David Daniels
    Scott David Daniels, Dec 29, 2005
    #6
    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. Mike John
    Replies:
    1
    Views:
    487
    Axel Dahmen
    Jun 29, 2003
  2. Kevin Spencer
    Replies:
    0
    Views:
    456
    Kevin Spencer
    Jun 30, 2003
  3. Mike John

    How can I refrence the aspNetEmail

    Mike John, Jul 6, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    392
    dave wanta
    Jul 6, 2003
  4. Greg Phillips
    Replies:
    1
    Views:
    399
    Victor Bazarov
    May 26, 2004
  5. =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

    refrence from return value ?

    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 29, 2004, in forum: C++
    Replies:
    8
    Views:
    406
    Sumit Rajan
    Sep 29, 2004
Loading...

Share This Page