Home > Enterprise >  Python: How to copy a multidimensional list by value without nested cycles?
Python: How to copy a multidimensional list by value without nested cycles?

Time:09-20

I have a multidimensional list with lots of data and I need to copy it to another variable.

Both

b = a.copy()

and

b = a[:][:][:][:]

don't help - I still have data in b bound to data in a

Of course, I can copy it element by element with nested cycles (maybe only saving one level with .copy()). But is there a better way?

PS: The situation gets even worse if my data are in dict of lists or dict of dicts. So is there some compact way to copy such nested structures?

CodePudding user response:

from copy import deepcopy

b = deepcopy(a)
  • Related