Duplicating list of lists [newbie]

yatsek at gmail.com yatsek at gmail.com
Sun Mar 23 11:34:20 EDT 2008


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



More information about the Python-list mailing list