Python: difference between shallow copy and deep copy

Assignment Statement (=)

  • Deep Copy
  • Shallow Copy

Shallow Copy

# Official DocumentA shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

Deep Copy

# Official Document
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Making a deep copy is slower

Conclusion

  • A deep copy will create a fully independent object from the original, but it is slower
  • A shallow copy will not clone a child object, thus, it is not fully independent.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store