Skip to content Skip to sidebar Skip to footer

Python Lists Copying Is It Deep Copy Or Shallow Copy And How Is It Done?

How is Deep copy being done in python for lists? I am a little confused for copying of lists. Is it using shallow copy or deep copy? Also, what is the syntax for sublists? is it

Solution 1:

The new list is a copy of references. g[0] and a[0] both reference the same object. Thus this is a shallow copy. You can see the copy module's deepcopy method for recursively copying containers, but this isn't a common operation in my experience.

Stylistically, I prefer the more explicit g = list(a) to create a copy of a list, but creating a full slice has the same effect.

Solution 2:

From Python Doc you have to use copy.deepcopy(x)

Post a Comment for "Python Lists Copying Is It Deep Copy Or Shallow Copy And How Is It Done?"