On Jan 24, 9:55 am, [EMAIL PROTECTED] 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 ki
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 spee
[EMAIL PROTECTED] 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]
>
Have a look at the copy module if you have a somewhat "complex" object graph
to duplicate. Remember, you're essentially just creating another reference
to a singular list object with simple assignment (a = b).
>>> a = [1,2,3,4, ['5a', '5b', '5c', ['6a', '6b','6c']], 7,8]
>>> b = a
>>> a
[1, 2, 3,
On Jan 24, 9:36 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
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
>