๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ copy.html
copy โ€” Shallow and deep copy operations
A shallow copy constructs a new ... found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ copy-python-deep-copy-shallow-copy
Deep Copy and Shallow Copy in Python - GeeksforGeeks
A deep copy creates a new compound object before inserting copies of the items found in the original into it in a recursive manner.
Published: February 5, 2026
Discussions

oop - How can I create a copy of an object in Python? - Stack Overflow
Hi @Sven Marnach, how many "depth ... can deepcopy() copy? 2023-10-28T15:57:28.067Z+00:00 ... Save this answer. ... Show activity on this post. So, if I change values of the fields of the new object, the old object should not be affected by that. You mean a mutable object then. In Python 3, lists get ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Beginner] Understanding shallow vs deep copy
First, G4G is generally crap. Don't waste your time with it. Second, they likely mean changes to objects held within the shallow copied object, and just worded it poorly: l = [ [1] ] # A list, holding a list, holding a 1 copy = l.copy() l[0][0] = 2 # Get the list inside the outer list, and mutate it by changing the 1 to a 2 print(copy) # [[2]] Because a shallow copy was made, both l and c are holding the same exact list, and l[0][0] = 2 altered that one list. I'm assuming this is what they meant; that mutations to elements will show in both the original and the copy, since they're holding the same objects. Compare that to a deepcopy: from copy import deepcopy l = [[1]] c = deepcopy(l) l[0][0] = 2 print(c) # [[1]] The deep copy also copied the inner elements, so the two outer lists now refer to different inner objects, so changes aren't reflected. More on reddit.com
๐ŸŒ r/learnpython
8
0
February 25, 2023
copy vs deepcopy
Copy doesn't do exactly what deepcopy does: >>> a = [[1,2,3]] >>> b = a.copy() >>> b[0].append(4) >>> a [[1, 2, 3, 4]] >>> from copy import deepcopy >>> b = deepcopy(a) >>> b[0].append(5) >>> a [[1, 2, 3, 4]] >>> b [[1, 2, 3, 4, 5]] the point is that deepcopy creates new objects for everything, so the inner list will be recreated too. Thus modifying the inner list on a deep copy will not affect the source of the deep copy. More on reddit.com
๐ŸŒ r/learnpython
5
6
November 6, 2020
Use case of the .copy() method
I am trying to understand how shallow copies are useful, if any operation I perform on them will still be applied to the original list. That's incorrect, it won't. Have you tried actually using and playing around with .copy()? In [1]: list_a = [1, 2, 3] In [2]: list_reassign = list_a In [3]: list_shallow_copy = list_a.copy() In [4]: list_a, list_reassign, list_shallow_copy Out[4]: ([1, 2, 3], [1, 2, 3], [1, 2, 3]) In [5]: list_a.append(4) In [6]: list_a, list_reassign, list_shallow_copy Out[6]: ([1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3]) More on reddit.com
๐ŸŒ r/learnpython
23
3
June 12, 2024
๐ŸŒ
Python for the Lab
pythonforthelab.com โ€บ blog โ€บ deep-and-shallow-copies-of-objects
Deep and Shallow Copies of Objects | Python For The Lab
If you change copy by deepcopy, the behavior would change, exactly in the same way than with lists or dictionaries. But we can go one step further, and customize the behavior of the shallow or deep copies of objects. With Python, you have a very high level of granularity regarding how much control you have on every step, including deep and shallow copies.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ shallow-deep-copy
Python Shallow Copy and Deep Copy (With Examples)
Letโ€™s continue with example 2. However, we are going to create deep copy using deepcopy() function present in copy module. The deep copy creates independent copy of original object and all its nested objects.
๐ŸŒ
The Python Coding Book
thepythoncodingbook.com โ€บ home โ€บ blog โ€บ shallow and deep copy in python and how to use __copy__()
Shallow and Deep Copy in Python and How to Use __copy__()
November 12, 2023 - Therefore, the references in teams and those in deepcopied_teams point to different objects. The two copies created by deepcopy() are entirely separate from each other. Hereโ€™s how this representation looks now: You can read more about shallow and deep copy in Python in the official documentation.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ glossary โ€บ deep-copy
deep copy | Python Glossary โ€“ Real Python
In Python, deep copy refers to creating a new object that is a complete, independent clone of the original object, including all objects it refers to, recursively.
๐ŸŒ
Real Python
realpython.com โ€บ copying-python-objects
How to Copy Objects in Python: Shallow vs Deep Copy Explained โ€“ Real Python
1 week ago - Deep copying recursively duplicates all objects, ensuring full independence from the original. Pythonโ€™s copy module provides the copy() function for shallow copies and deepcopy() for deep copies.
Find elsewhere
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Shallow and Deep Copy in Python: copy(), deepcopy() | note.nkmk.me
May 13, 2023 - When objects are contained within mutable objects, like elements in a list or values in a dictionary, a shallow copy creates references to the original objects, while a deep copy creates new copies of the original objects.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-make-a-deep-copy-in-python
How to make a deep copy in Python
The deepcopy() function of Python's copy module can be used in order to carry out deep copying. There are two main ways to copy objects in Python:
Top answer
1 of 4
346

To get a fully independent copy of an object you can use the copy.deepcopy() function.

For more details about shallow and deep copying please refer to the other answers to this question and the nice explanation in this answer to a related question.

2 of 4
177

How can I create a copy of an object in Python?

So, if I change values of the fields of the new object, the old object should not be affected by that.

You mean a mutable object then.

In Python 3, lists get a copy method (in 2, you'd use a slice to make a copy):

>>> a_list = list('abc')
>>> a_copy_of_a_list = a_list.copy()
>>> a_copy_of_a_list is a_list
False
>>> a_copy_of_a_list == a_list
True

Shallow Copies

Shallow copies are just copies of the outermost container.

list.copy is a shallow copy:

>>> list_of_dict_of_set = [{'foo': set('abc')}]
>>> lodos_copy = list_of_dict_of_set.copy()
>>> lodos_copy[0]['foo'].pop()
'c'
>>> lodos_copy
[{'foo': {'b', 'a'}}]
>>> list_of_dict_of_set
[{'foo': {'b', 'a'}}]

You don't get a copy of the interior objects. They're the same object - so when they're mutated, the change shows up in both containers.

Deep copies

Deep copies are recursive copies of each interior object.

>>> lodos_deep_copy = copy.deepcopy(list_of_dict_of_set)
>>> lodos_deep_copy[0]['foo'].add('c')
>>> lodos_deep_copy
[{'foo': {'c', 'b', 'a'}}]
>>> list_of_dict_of_set
[{'foo': {'b', 'a'}}]

Changes are not reflected in the original, only in the copy.

Immutable objects

Immutable objects do not usually need to be copied. In fact, if you try to, Python will just give you the original object:

>>> a_tuple = tuple('abc')
>>> tuple_copy_attempt = a_tuple.copy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'copy'

Tuples don't even have a copy method, so let's try it with a slice:

>>> tuple_copy_attempt = a_tuple[:]

But we see it's the same object:

>>> tuple_copy_attempt is a_tuple
True

Similarly for strings:

>>> s = 'abc'
>>> s0 = s[:]
>>> s == s0
True
>>> s is s0
True

and for frozensets, even though they have a copy method:

>>> a_frozenset = frozenset('abc')
>>> frozenset_copy_attempt = a_frozenset.copy()
>>> frozenset_copy_attempt is a_frozenset
True

When to copy immutable objects

Immutable objects should be copied if you need a mutable interior object copied.

>>> tuple_of_list = [],
>>> copy_of_tuple_of_list = tuple_of_list[:]
>>> copy_of_tuple_of_list[0].append('a')
>>> copy_of_tuple_of_list
(['a'],)
>>> tuple_of_list
(['a'],)
>>> deepcopy_of_tuple_of_list = copy.deepcopy(tuple_of_list)
>>> deepcopy_of_tuple_of_list[0].append('b')
>>> deepcopy_of_tuple_of_list
(['a', 'b'],)
>>> tuple_of_list
(['a'],)

As we can see, when the interior object of the copy is mutated, the original does not change.

Custom Objects

Custom objects usually store data in a __dict__ attribute or in __slots__ (a tuple-like memory structure.)

To make a copyable object, define __copy__ (for shallow copies) and/or __deepcopy__ (for deep copies).

from copy import copy, deepcopy

class Copyable:
    __slots__ = 'a', '__dict__'
    def __init__(self, a, b):
        self.a, self.b = a, b
    def __copy__(self):
        return type(self)(self.a, self.b)
    def __deepcopy__(self, memo): # memo is a dict of id's to copies
        id_self = id(self)        # memoization avoids unnecesary recursion
        _copy = memo.get(id_self)
        if _copy is None:
            _copy = type(self)(
                deepcopy(self.a, memo), 
                deepcopy(self.b, memo))
            memo[id_self] = _copy 
        return _copy

Note that deepcopy keeps a memoization dictionary of id(original) (or identity numbers) to copies. To enjoy good behavior with recursive data structures, make sure you haven't already made a copy, and if you have, return that.

So let's make an object:

>>> c1 = Copyable(1, [2])

And copy makes a shallow copy:

>>> c2 = copy(c1)
>>> c1 is c2
False
>>> c2.b.append(3)
>>> c1.b
[2, 3]

And deepcopy now makes a deep copy:

>>> c3 = deepcopy(c1)
>>> c3.b.append(4)
>>> c1.b
[2, 3]
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ deep-copy-python
Deep Copy in Python: Syntax, Examples & Applications
January 24, 2023 - It will create a new object with ... nested objects and lists. A deep copy in python is a copy of an object that is completely independent of the original object....
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ python tutorial โ€บ python deep copy
Python deep copy | Complete Guide to Python deep copy with Examples
September 29, 2023 - So to overcome this limitation, we used the deep copy function and created list b, which is a copy of list a, and changes are done in list a; do not alter or change the values because it is an entirely independent and unique object. deep copy function can be called after importing the package copy. Both deepcopy and shallowcopy can be reached from the package copy. We can use import copy or from copy import deep copy. We have seen in detail the Python deepcopy that is most popular in the Python programming platform because of its importance in working with duplicates or copies of original objects where the user can create a duplicate copy of an actual thing and make it independent from the original object to perform multiple operations or functions using the exact copy.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ copy
copy โ€“ Duplicate objects - Python Module of the Week
__copy__() is called without any arguments and should return a shallow copy of the object. __deepcopy__() is called with a memo dictionary, and should return a deep copy of the object.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-deep-and-shallow-copy-object
Python: Deep and Shallow Copy Object
September 27, 2021 - We also learned that we can use the copy() method of the copy module to create a shallow copy, and the deepcopy() method to create a deep copy of the compound objects. ... Get tutorials, guides, and dev jobs in your inbox. No spam ever. Unsubscribe at any time.
๐ŸŒ
Python Geeks
pythongeeks.org โ€บ python geeks โ€บ learn python โ€บ python deep copy and shallow copy
Python Deep Copy and Shallow Copy - Python Geeks
February 24, 2023 - Hence the copy is not fully independent of the actual object. 2. A deep copy on the other hand recursively clones a child object.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-copy-objects-in-python
How to Copy Objects in Python
April 17, 2025 - In this tutorial, you learned about copying objects in Python. Specifically, we talked about: How the assignment operator = is used for referencing and not copying. The built-in copy module, which provides functions that allow us to shallow copy and deep copy the objects in our program.
๐ŸŒ
Pythontic
pythontic.com โ€บ modules โ€บ copy โ€บ deepcopy
deepcopy function in Python | Pythontic.com
Deep Copying of objects is supported in Python through the deepcopy() function of the โ€œcopyโ€ module.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-deepcopy
Python Deepcopy Syntax and Usage Guide (With Examples)
February 5, 2024 - In Python, a deep copy generates a new and independent copy of an entire object or list, complete with its unique memory address. Consequently, modifications to the original object or list do not impact the copied object or list, and vice versa.
๐ŸŒ
Medium
medium.com โ€บ @ihuomacbasil โ€บ copy-in-python-deep-copy-and-shallow-copy-59b51bda7cde
copy in python(Deep copy and shallow copy) | by Basil Ihuoma | Medium
December 28, 2019 - In case of deep copy, a copy of object is copied in other object. It means that any changes made to a copy of object do not reflect in the original object. In python, this is implemented using โ€œdeepcopy()โ€ function.