0

Here is a minimal reproducible code to give an example:

L = [1]
M = [1]
N = [L, M]
from copy import deepcopy
A = deepcopy(N)
A[0][0] = "ll"
L  # I want this to become ["ll"]
Out[8]: [1]
A
Out[9]: ["ll", [1]]

I would want to make a deepcopy of N so that when I modify A (adding an element, removing one), it doesn't affect N. This works. But I also want the following behaviour: if I modify N[0] i.e. L, I want it to modify A[0] too because I would want those two pointers to point at the same thing.

Is this achievable other than with:

A = [n for n in N]

? Is it bad practice to do so?

Here is a minimal reproducible example to show that it works:

L = [1]
M = [1]
N = [L, M]
A = [n for n in N]
A[0][0] = "ll"
L
Out[7]: ['ll']
A.remove(L)
A
Out[9]: [[1]]
N
Out[10]: [['ll'], [1]]

If not, what are the good practices regarding such a problem?

0 Answers0