python, how copy the items inside the list ... when items here other list ?

using
LIST.copy()
not work

it copy (make a new obj) only of the “container” , but if inside the list there other list ?

what i mean :

>>> L=[[[1,2,3]]]
>>> L2=L.copy()
>>> L2[0][0] = “ww”
>>> L
[[‘ww’]] # bad!

i want istead that L2 is a new container, that copy all … something as (the syntax is invented):

>>> L=[[[1,2,3]]]
>>> L2=L.superCopy()
>>> L2[0][0] = “ww”
>>> L
[[[1,2,3]]]

is possible ? or is necessary make it manually (item by item) ?

this is the piece of code that give issue, solved “manually”


    else:
        if lenL < 10 :
            if random.random() > 0.5 :
                cop = L[int(lenL * random.random())].copy()
                cop2 = [cop[0].copy(), cop[1].copy() ] #<<<< line redundant and prone to bug
                L.append( cop2 ) #????

Look for deepcopy :wink:

Thanks Monster it work …also for Vector and Matrices . :slight_smile:

>>> import copy
>>>
>>> L = [[[1, 2, 3]]]
>>> L2 = copy.deepcopy(L)
>>> L[0][0]=“ww”
>>> L2
[[[1, 2, 3]]]