How to make a deep copy in Python

Deep copy in Python is a process where each object or nested object gets copied entirely. In simple words, this means that any changes made to the deep copy will not reflect on the original one as they have saved separately at different memory locations. The deepcopy() function of Python's copy module can be used in order to carry out deep copying.

Copy an object in Python

There are two main ways to copy objects in Python:

  1. Shallow copy: Only copies the reference of nested objects.

  2. Deep copy: Copies the entire object, including nested ones, to create an independent copy.

To show the difference between shallow and deep copy, consider a list of lists in Python. A shallow copy would create a new object but still reference the nested objects. However, A deep copy creates entirely new copies of the nested objects, preventing any modification from affecting the original object.

Shallow copy vs deep copy
Shallow copy vs deep copy

In this Answer, we’ll focus on Python deep copy, explaining its definition and syntax and providing examples to understand how it works.

What is a deep copy in Python?

In Python, a deep copy ensures that a new object is created for each object in the original. This is particularly useful when the object you’re copying contains other objects, such as lists or dictionaries. By using deep copy, you can ensure that modifying the copied object won’t impact the original object or any of its nested elements.

Syntax of Python deep copy

The deepcopy() function is part of the copy module in Python. Here is its syntax:

import copy
deep_copy_object = copy.deepcopy(object_to_copy)
  • object_to_copy: This is the object you want to deep copy.

  • deep_copy_object: The newly created object that is a deep copy of the original.

Example of Python deep copy

Here’s another example of how Python deep copy works when copying objects that contain nested objects like dictionaries.

import copy
# Original dictionary with nested dictionaries
original_dict = {
'name': 'Alice',
'grades': {
'math': 90,
'science': 85,
'history': {'midterm': 88, 'final': 92}
}
}
# Perform a deep copy
deep_copy_dict = copy.deepcopy(original_dict)
# Modify the deep copied dictionary
deep_copy_dict['grades']['math'] = 95
deep_copy_dict['grades']['history']['midterm'] = 93
# Print results to show the effect of changes
print("Original Dictionary:", original_dict)
print("Deep Copied Dictionary:", deep_copy_dict)

In this example, the changes to the deep_copy_dict do not affect the original_dict. The deep copy ensures that both dictionaries are stored in different memory locations.

Tip for learners: Try adding more nested levels or modify the original dictionary after copying. For example, change original_dict['grades']['science'] = 88 and see if it impacts deep_copy_dict. This experiment shows how deep copies behave independently from the original.

Memory and performance

  1. Deep copying uses more memory and takes more time than shallow copying. This difference can matter when working with large or complex data and when deep copying isn’t necessary.

  2. For data that won’t be changed (immutable data), a shallow copy is usually faster and more efficient since it doesn’t duplicate data. However, for data that can change (mutable objects), deep copying is safer because it keeps changes to the copy from affecting the original data.

Key Takeaways:

  • Deep copy definition: In Python, deep copy creates an entirely new object, including all nested objects, ensuring that changes to the copied object do not affect the original.

  • Difference between shallow and deep copy: A shallow copy copies the reference of nested objects, while a deep copy creates independent copies of both the outer and inner objects.

  • How to perform deep copy: Python’s deepcopy() function, available in the copy module, allows you to create a deep copy of any object, ensuring separate memory allocation.

Ready to deepen your Python knowledge? Start our “Become a Python Developer” path, beginning with Python’s basic concepts and advancing to more complex topics, preparing you for a career as a Python developer.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is a shallow copy in Python?

A shallow copy in Python creates a new object, but it only copies references to the original nested objects. Changes to nested elements in the copied object will affect the original.


How does shallow copy work in Python?

In a shallow copy, the top-level object is copied, but its nested elements (like lists within lists) are referenced, not duplicated. Modifying nested elements in the shallow copy impacts the original object.


Is clone() a deep copy?

In Python, the clone() method doesn’t exist like in some other languages; instead, the copy() method makes a shallow copy, and deepcopy() from the copy module is used for deep copies. Unlike Java, where clone() might not always create a deep copy and can share references, Python separates these functions to give developers clear options for copying objects based on their needs.


What’s the best way to start learning to code?

The most effective way is to use an interactive platform with hands-on lessons. Educative’s “Learn to Code” skill path takes you from a beginner to a job-ready programmer through gamified lessons and project-based learning.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved