Pass a list to diffrerent variables.

Discussion in 'Python' started by user@domain.invalid, May 2, 2004.

  1. Guest

    When trying to pass the contents from one list to another this happens:

    list = [1,2,3]
    list1 = list
    print list1
    [1,2,3]
    list.append(7)
    print list1
    [1,2,3,7]

    Whats the easiest way to pass the data in a list, not the pointer, to
    another variable
    , May 2, 2004
    #1
    1. Advertising

  2. Peter Otten Guest

    lid wrote:

    > When trying to pass the contents from one list to another this happens:
    >
    > list = [1,2,3]


    Don't use list as a name. It hides the builtin list class.

    > list1 = list
    > print list1
    > [1,2,3]
    > list.append(7)
    > print list1
    > [1,2,3,7]
    >
    > Whats the easiest way to pass the data in a list, not the pointer, to
    > another variable


    >>> first = [1, 2, 3]
    >>> second = list(first) # create a list from the sequence 'first'
    >>> second.append(4)
    >>> first

    [1, 2, 3]
    >>> third = first[:] # slice comprising all items of the 'first' list
    >>> third.append(5)
    >>> first

    [1, 2, 3]
    >>>


    Both methods shown above result in a (shallow) copy of the original list.

    Peter
    Peter Otten, May 2, 2004
    #2
    1. Advertising

  3. Robbie Guest

    Peter Otten wrote:

    > lid wrote:
    >
    >
    >>When trying to pass the contents from one list to another this happens:
    >>
    >>list = [1,2,3]

    >
    >
    > Don't use list as a name. It hides the builtin list class.
    >
    >
    >>list1 = list
    >>print list1
    >> [1,2,3]
    >>list.append(7)
    >>print list1
    >> [1,2,3,7]
    >>
    >>Whats the easiest way to pass the data in a list, not the pointer, to
    >>another variable

    >
    >
    >>>>first = [1, 2, 3]
    >>>>second = list(first) # create a list from the sequence 'first'
    >>>>second.append(4)
    >>>>first

    >
    > [1, 2, 3]
    >
    >>>>third = first[:] # slice comprising all items of the 'first' list
    >>>>third.append(5)
    >>>>first

    >
    > [1, 2, 3]
    >
    >
    > Both methods shown above result in a (shallow) copy of the original list.
    >
    > Peter


    Thanks, that works fine but I am working with a 2d list...
    and I dont understand why this happens
    d = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
    d
    [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    f = list(d)
    f
    [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    d[0][0]="a"
    d
    [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    f
    [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    What exactly is this doing? And how can I stop it?
    Robbie, May 2, 2004
    #3
  4. Peter Otten Guest

    Robbie wrote:

    > Thanks, that works fine but I am working with a 2d list...
    > and I dont understand why this happens
    > d = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
    > d
    > [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > f = list(d)
    > f
    > [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > d[0][0]="a"
    > d
    > [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > f
    > [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > What exactly is this doing? And how can I stop it?


    While you have copied the outer list, both d and f share the same items, e.
    g. d[0] and f[0] refer to the same item (a list containing 1,2,3 in this
    case). That is called a "shallow" copy. To avoid such sharing, you need to
    copy not only the outer list but also recursively the data it contains.
    This is called a "deep" copy and can be done with the copy module:

    >>> import copy
    >>> a = [[1,2,3], [4,5]]
    >>> b = copy.deepcopy(a)
    >>> a[0][0] = "a"
    >>> b[0][0]

    1
    >>>


    A word of warning: I've never used this module in my code and think its
    usage is a strong indication of a design error in your application. (Of
    course I cannot be sure without knowing what you actually try to achieve.)

    Peter
    Peter Otten, May 2, 2004
    #4
  5. On 2004-05-02, lid <> wrote:
    > When trying to pass the contents from one list to another this happens:
    >
    > list = [1,2,3]
    > list1 = list
    > print list1
    > [1,2,3]
    > list.append(7)
    > print list1
    > [1,2,3,7]
    >
    > Whats the easiest way to pass the data in a list, not the pointer, to
    > another variable


    Try list1 = list[:] instead. This creates a copy.

    D.
    Dominique Orban, May 2, 2004
    #5
  6. Jon Willeke Guest

    Robbie wrote:
    > Peter Otten wrote:
    >
    >> Both methods shown above result in a (shallow) copy of the original list.

    >
    > Thanks, that works fine but I am working with a 2d list...
    > and I dont understand why this happens
    > d = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
    > d
    > [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > f = list(d)
    > f
    > [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > d[0][0]="a"
    > d
    > [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > f
    > [['a', 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
    > What exactly is this doing? And how can I stop it?


    That's why Peter cautioned that the list() constructor yields a shallow
    copy. The is operator and the id() function will reveal that d[0][0]
    and f[0][0] are the same list. You want the copy.deepcopy() function.
    Jon Willeke, May 2, 2004
    #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. Ben Kial
    Replies:
    1
    Views:
    643
    Eric Enright
    Nov 15, 2004
  2. Replies:
    9
    Views:
    935
  3. KRB
    Replies:
    1
    Views:
    297
    Nobody
    Apr 7, 2012
  4. Alex Vinokur

    Different C-preprocessors have diffrerent behavior

    Alex Vinokur, Jul 15, 2012, in forum: C Programming
    Replies:
    1
    Views:
    394
    Alan Curry
    Jul 15, 2012
  5. Alex Vinokur
    Replies:
    2
    Views:
    284
    Alex Vinokur
    Dec 19, 2012
Loading...

Share This Page