Duplicating a variable

Discussion in 'Python' started by hnessenospam@yahoo.com, Jan 24, 2008.

  1. Guest

    I have run into a bit of a subtle problem. How do I go about
    duplicating a variable (particularly a list of lists) in python. I
    was surprised when simple assignment didn't work. For example, let y =
    [1,2,3]

    >>> x = y
    >>> x[2] = 5
    >>> y

    [1,2,5]

    It seems that simply assigning x to y allows further modification of y
    via x. (I'm new to python and I'm sure this is obvious to experienced
    users). So my question, how do I go about duplicating a variable
    which I can then manipulate independently?

    Thanks,

    -Hans
     
    , Jan 24, 2008
    #1
    1. Advertising

  2. Guest

    On Jan 24, 9:36 am, "" <>
    wrote:
    > I have run into a bit of a subtle problem. How do I go about
    > duplicating a variable (particularly a list of lists) in python. I
    > was surprised when simple assignment didn't work. For example, let y =
    > [1,2,3]
    >
    > >>> x = y
    > >>> x[2] = 5
    > >>> y

    >
    > [1,2,5]
    >
    > It seems that simply assigning x to y allows further modification of y
    > via x. (I'm new to python and I'm sure this is obvious to experienced
    > users). So my question, how do I go about duplicating a variable
    > which I can then manipulate independently?
    >
    > Thanks,
    >
    > -Hans


    Ah, found the copy module... Much better. Thanks,

    -Hans
     
    , Jan 24, 2008
    #2
    1. Advertising

  3. a écrit :
    > I have run into a bit of a subtle problem. How do I go about
    > duplicating a variable (particularly a list of lists) in python.


    using the deepcopy function of the copy module.

    > I
    > was surprised when simple assignment didn't work. For example, let y =
    > [1,2,3]
    >
    >>>> x = y
    >>>> x[2] = 5
    >>>> y

    > [1,2,5]


    Python only uses object references. It never copy anything unless
    explicitely asked for.
     
    Bruno Desthuilliers, Jan 24, 2008
    #3
  4. Guest

    Hans:
    > I have run into a bit of a subtle problem. How do I go about
    > duplicating a variable (particularly a list of lists) in python. I
    > was surprised when simple assignment didn't work.


    Python is quite high-level language, but now and then it too accepts
    some compromises to increase its speed/size performance. Probably
    after Python someone will invent a language that may be slower than
    Python because it's higher level than Python, and avoids that problem
    you talk about (and other things). (And with a strategy of smart data
    sharing and copy-on-write the interpreter can avoid part of that
    overhead).


    > how do I go about duplicating a variable
    > which I can then manipulate independently?


    If your variable contains a list, then you can copy it like this:

    >>> l1 = [1, 2, 3]
    >>> l2 = l1[:]
    >>> l2[1] = 4


    As you can see now they are two distinct lists:

    >>> l1

    [1, 2, 3]
    >>> l2

    [1, 4, 3]

    If you want to copy any kind of object you can use the copy function
    (instead of a simpler copy method that's absent):

    >>> d1 = {1:2, 3:4}
    >>> from copy import copy
    >>> d2 = copy(d1)
    >>> d1[1] = 5
    >>> d1

    {1: 5, 3: 4}
    >>> d2

    {1: 2, 3: 4}

    But as you can see copy works only one level deep:

    >>> d3 = {1:[1], 3:4}
    >>> d3

    {1: [1], 3: 4}
    >>> d4 = copy(d3)
    >>> d3[1][0] = 2
    >>> d3

    {1: [2], 3: 4}
    >>> d4

    {1: [2], 3: 4}

    To copy all levels you need deepcopy:

    >>> from copy import deepcopy
    >>> d5 = deepcopy(d3)
    >>> d3[1][0] = 5
    >>> d3

    {1: [5], 3: 4}
    >>> d4

    {1: [5], 3: 4}
    >>> d5

    {1: [2], 3: 4}

    Bye,
    bearophile
     
    , Jan 24, 2008
    #4
  5. Guest

    On Jan 24, 9:55 am, wrote:
    >
    > If your variable contains a list, then you can copy it like this:
    >
    > >>> l1 = [1, 2, 3]
    > >>> l2 = l1[:]
    > >>> l2[1] = 4

    >
    > As you can see now they are two distinct lists:
    >
    > >>> l1

    > [1, 2, 3]
    > >>> l2

    >
    > [1, 4, 3]
    >
    > If you want to copy any kind of object you can use the copy function
    > (instead of a simpler copy method that's absent):
    >
    > >>> d1 = {1:2, 3:4}
    > >>> from copy import copy
    > >>> d2 = copy(d1)
    > >>> d1[1] = 5
    > >>> d1

    > {1: 5, 3: 4}
    > >>> d2

    >
    > {1: 2, 3: 4}
    >
    > But as you can see copy works only one level deep:
    >
    > >>> d3 = {1:[1], 3:4}
    > >>> d3

    > {1: [1], 3: 4}
    > >>> d4 = copy(d3)
    > >>> d3[1][0] = 2
    > >>> d3

    > {1: [2], 3: 4}
    > >>> d4

    >
    > {1: [2], 3: 4}
    >
    > To copy all levels you need deepcopy:
    >
    > >>> from copy import deepcopy
    > >>> d5 = deepcopy(d3)
    > >>> d3[1][0] = 5
    > >>> d3

    > {1: [5], 3: 4}
    > >>> d4

    > {1: [5], 3: 4}
    > >>> d5

    >
    > {1: [2], 3: 4}
    >
    > Bye,
    > bearophile


    Works great, it is exactly what I needed thanks!

    -Hans
     
    , Jan 24, 2008
    #5
    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. AC
    Replies:
    0
    Views:
    534
  2. Don

    Duplicating datasheet screens

    Don, Feb 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    361
  3. Todd Plambeck

    ASP.Net Duplicating DB Entries

    Todd Plambeck, Aug 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    504
  4. George Jempty

    log4j duplicating appender entries

    George Jempty, Jan 7, 2005, in forum: Java
    Replies:
    2
    Views:
    2,491
  5. johkar
    Replies:
    0
    Views:
    343
    johkar
    Aug 8, 2003
Loading...

Share This Page