Duplicating list of lists [newbie]

Discussion in 'Python' started by yatsek@gmail.com, Mar 23, 2008.

  1. Guest

    Hi there.
    I felt like I already know my toys around but it looks that I'm still
    newbie on some points.

    So here goes problem:

    Lets say that we have list to copy:

    a = [1,2,3]
    b = a[:]
    a[0] = 5

    a
    > [5,2,3]

    b
    >[1,2,3]


    So far so good

    But... let's say that "a" also contains lists:

    a = [1,2,[5,6,7]]
    b = a[:]

    a[0] = 55
    a
    > [55,2,[5,6,7]]

    b
    > [1,2,[5,6,7]]


    Looks OK but...

    a[2][0] = 99
    a
    > [55,2,[99,6,7]]

    b
    > [1,2,[99,6,7]]


    So - it looks that in list "b" there copy of all objects from list "a"
    including not copy of list [5,6,7] but reference to it.

    Is there simple way to copy a into b (like a[:]) with all copies of
    all objects going as deep as possible? Or it can be done only
    manually?

    Regards
    Yatsek
     
    , Mar 23, 2008
    #1
    1. Advertising

  2. On Mar 23, 11:34 am, wrote:
    > Hi there.
    > I felt like I already know my toys around but it looks that I'm still
    > newbie on some points.
    >
    > So here goes problem:
    >
    > Lets say that we have list to copy:
    >
    > a = [1,2,3]
    > b = a[:]
    > a[0] = 5
    >
    > a
    >
    > > [5,2,3]

    > b
    > >[1,2,3]

    >
    > So far so good
    >
    > But... let's say that "a" also contains lists:
    >
    > a = [1,2,[5,6,7]]
    > b = a[:]
    >
    > a[0] = 55
    > a
    >
    > > [55,2,[5,6,7]]

    > b
    > > [1,2,[5,6,7]]

    >
    > Looks OK but...
    >
    > a[2][0] = 99
    > a
    >
    > > [55,2,[99,6,7]]

    > b
    > > [1,2,[99,6,7]]

    >
    > So - it looks that in list "b" there copy of all objects from list "a"
    > including not copy of list [5,6,7] but reference to it.


    Right; that's what it's called a "shallow copy".

    > Is there simple way to copy a into b (like a[:]) with all copies of
    > all objects going as deep as possible?


    Yes, there is:

    from copy import deepcopy
    b = deepcopy(a)


    HTH,
    George
     
    George Sakkis, Mar 23, 2008
    #2
    1. Advertising

  3. > On Behalf Of
    > So - it looks that in list "b" there copy of all objects from list "a"
    > including not copy of list [5,6,7] but reference to it.
    >
    > Is there simple way to copy a into b (like a[:]) with all
    > copies of all objects going as deep as possible? Or it can be
    > done only manually?


    I'd suggest checking out copy.deepcopy.

    >>> a = [1, [1, 2, 3], 2]
    >>> b = a[:]
    >>> a[1][2] = 'spam'
    >>> b

    [1, [1, 2, 'spam'], 2]
    >>> from copy import deepcopy
    >>> b = deepcopy(a)
    >>> a[1][2] = 'deepcopy is your friend'
    >>> b

    [1, [1, 2, 'spam'], 2]

    Regards,
    Ryan Ginstrom
     
    Ryan Ginstrom, Mar 23, 2008
    #3
  4. Guest

    Yatsek:
    > Is there simple way to copy a into b (like a[:]) with all copies of
    > all objects going as deep as possible?


    If you only want to copy up to level-2, then you can do something
    like:
    cloned = [subl[:] for subl in somelist]
    Or sometimes safer:
    cloned = [list(subl) for subl in somelist]

    If you want to go all the way down, you can use the deepcopy function
    of the copy module. In many situations the level-2 copy may be enough,
    and it can be faster.

    Bye,
    bearophile
     
    , Mar 23, 2008
    #4
    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. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. yomgui

    list of lists of lists ....

    yomgui, Jul 28, 2006, in forum: Python
    Replies:
    6
    Views:
    315
    yomgui
    Jul 31, 2006
  3. ardief
    Replies:
    14
    Views:
    743
    Paddy
    Feb 3, 2007
  4. bahoo
    Replies:
    3
    Views:
    307
    Bruno Desthuilliers
    Apr 3, 2007
  5. Sherm Pendley
    Replies:
    11
    Views:
    242
Loading...

Share This Page