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.

Answer from Sven Marnach on Stack Overflow
🌐
Python
docs.python.org › 3 › library › copy.html
copy — Shallow and deep copy operations
Return a deep copy of obj. ... Creates a new object of the same type as obj, replacing fields with values from changes.
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]
🌐
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 - Whenever you pass an immutable object to copy.copy(), it returns the object itself. You can read more about Object-Oriented Programming here: The chapter about Object-Oriented Programming in Python in The Python Coding Book
🌐
Python for the Lab
pythonforthelab.com › blog › deep-and-shallow-copies-of-objects
Deep and Shallow Copies of Objects | Python For The Lab
Copying objects in Python seems like a trivial task, but it can have unexpected implications in your programs. Copying data may be achieved by either duplicating the data or by storing references to the objects, having a much lower impact on the memory.
🌐
GeeksforGeeks
geeksforgeeks.org › python › copy-python-deep-copy-shallow-copy
Deep Copy and Shallow Copy in Python - GeeksforGeeks
In Python, assignment statements create references to the same object rather than copying it. Python provides the copy module to create actual copies which offer functions for shallow (copy.copy()) and deep (copy. deepcopy ()) copies.
Published: February 5, 2026
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Object_copying
Object copying - Wikipedia
July 18, 2026 - In OCaml, the library function Oo.copy performs shallow copying of an object. In Python, the library's copy module provides shallow copy and deep copy of objects through the copy() and deepcopy() functions, respectively.
🌐
Reddit
reddit.com › r/learnpython › shallow and deep copy
r/learnpython on Reddit: Shallow and Deep Copy
May 18, 2020 -

I am fairly new to the Python realm. What is the difference between shallow and deep copy? I know the theoritical part of it but Im not actually able to understand while practicing it.

1)Does the list.copy() work on shallow copy technique? How does it work?

2)How does it differ from the other copying techniques like the = operator; slicing [:] ; list() constructor?

I have experimented it on the interactive python using two lists list1 and list2. When using "List2 = List1", I was able to see changes I made to the List2 on the List1. Whereas copying the contents of List1 to the List2 using " List2 = List1.copy()" , didn't do so.

Please enlighten me! Thanks in advance!

🌐
W3Schools
w3schools.com › python › ref_module_copy.asp
Python copy Module
The copy module provides shallow and deep copy operations for arbitrary Python objects.
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch05s12.html
Making a Fast Copy of an Object - Python Cookbook [Book]
July 19, 2002 - When you need a copy, you explicitly ask for it, ideally with the copy.copy function, which knows how to copy built-in types, has reasonable defaults for your own objects, and lets you customize the copying process by defining a special method _ _copy_ _ in your own classes.
Authors: Alex MartelliDavid Ascher
Published: 2002
Pages: 608
🌐
W3Schools
w3schools.com › python › python_lists_copy.asp
Python - Copy Lists
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How do you copy an object in Python? - Python - Data Science Dojo Discussions
November 9, 2022 - In Python, the assignment operator (=) creates a reference between the target variable and the object, rather than creating a new copy of the object. To create a copy of an object, you can use the copy module. Two types of copies can be created ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-copy-objects-in-python
How to Copy Objects in Python
April 17, 2025 - Basic knowledge of Python programming, especially (for this tutorial), Function id(): Outputs the memory address of the object passed as argument. Data structures: Dictionaries and Lists. Modules: importing and using them in the program. Basic understanding of methods and functions. ... The copy module is an in-built module in Python which is primarily used for copying objects in Python.
🌐
LinkedIn
linkedin.com › pulse › how-copy-object-python-lupamudra-dey-gwslc
How to Copy an Object in Python
Login to LinkedIn to keep in touch with people you know, share ideas, and build your career.
🌐
Facebook
facebook.com › freecodecamp › posts › the-copy-module-is-a-built-in-module-in-python-that-you-can-use-to-copy-objects- › 722788397000597
(M) How to Copy Objects in Python By Sara Jadhav
Freecodecamp. 309,134 followers · 11,527 talking about this. freeCodeCamp is a community of millions of people around the world who are learning to code together.
🌐
DEV Community
dev.to › suvhotta › understanding-object-copying-in-python-19k1
Understanding Object Copying in Python - DEV Community
November 17, 2023 - That's why when the object itself was getting modified, I was getting altered values in the original_list in my code. Python provides 2 ways of doing so: 1) Shallow Copy 2) Deep Copy Both of these are available in the copy module provided as part of Python's standard library.
🌐
Real Python
realpython.com › python-copy
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.
🌐
Scaler
scaler.com › home › topics › copy in python
Copy in Python - Scaler Topics
May 8, 2024 - Using the deepcopy() function of the copy module can provide a real/deep clone of the object. Choose from our industry-leading programs designed for career success ... The syntax of the copy() method in python is simple.
🌐
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.