TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Shallow Copy vs. Deep Copy in Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python OOPPython Tutorials

Shallow Copy vs. Deep Copy in Python

Last updated: Sep 28, 2023 6:54 pm
By Meenakshi Agarwal
Share
2 Min Read
Shallow Copy vs. Deep Copy in Python
Shallow Copy vs. Deep Copy in Python
SHARE

From this tutorial, you will be learning about the differences between Shallow Copy and Deep Copy (Shallow Copy vs. Deep Copy) in Python.

Contents
The difference between Shallow and Deep copyHow to perform a shallow and deep copy?Program ExampleKey Differences

Note: The syntax used in the below section is for Python 3. You may change it to use a different version of Python.

Shallow Copy vs. Deep Copy

Must Read – 9 Ways to Copy a File in Python

The difference between Shallow and Deep copy

A shallow copy is one that makes a new object store the reference of another object. While, in deep copy, a new object stores the copy of all references of another object making it another list separate from the original one.

Thus, when you make a change to the deep copy of a list, the old list doesn’t get affected and vice-versa. But shallow copying causes changes in both the new as well as in the old list.

This copy method is applicable in compound objects such as a list containing another list.

The diagram shown below represents the difference between the shallow copy and the deep copy.

Shallow Copy vs. Deep Copy
Shallow vs. Deep copy

How to perform a shallow and deep copy?

When creating a shallow copy, use the assignment operator(=) to create them.

With the Copy module, you can create a shallow copy using the below syntax:

import copy
copy.copy(object_name)

For a deep copy, the following code can be used:

import copy
copy.deepcopy(object_name)

In the next section, a few programs are implemented to demonstrate the Copy Module in Python 3.

Program Example

Here is a simple program to demonstrate the difference between Shallow and Deep copy.

import copy

a = [ [1, 2, 3], [4, 5, 6] ]
b = copy.copy(a)

c = [ [7, 8, 9], [10, 11, 12] ]
d = copy.deepcopy(c)

print(a)
print(b)

a[1][2] = 23
b[0][0] = 98

print(a)
print(b)

print("\n")

print(c)
print(d)

a[1][2] = 23
b[0][0] = 98

print(c)
print(d)

The output will come as:

Example output

Key Differences

Here is a table summarizing the key differences between shallow copy and deep copy in Python:

FeatureShallow CopyDeep Copy
What it doesCreates a new object that references the same objects as the original object.Creates a new object that contains copies of all the objects referenced by the original object.
When to use itWhen you only need to copy the top-level structure of an object.When you need to copy the entire object hierarchy, including nested objects.
SpeedFasterSlower
Memory usageLess memory-intensiveMore memory-intensive
AvailabilityAvailable in the standard libraryRequires the copy module

Quick Summary

Here is a simple explanation of each feature:

  • What it does: A shallow copy creates a new object that references the same objects as the original object. This means that changes made to the original object will also be reflected in the copy. For example, if you have a list of numbers and you shallow copy it, any changes you make to the list in the copy will also be made to the original list.
  • When to use it: You should use shallow copy when you only need to copy the top-level structure of an object. This is usually the case when you are copying simple objects, such as lists or strings.
  • Speed: Shallow copy is faster than deep copy because it does not need to create copies of all the objects referenced by the original object.
  • Memory usage: Shallow copy is less memory-intensive than deep copy because it does not need to create copies of all the objects referenced by the original object.
  • Availability: A shallow copy is available in the standard library, so you do not need to install any additional modules to use it. Deep copy requires the copy module, which is a standard library module.

We hope this helps! Let us know if you have any other questions.

Best,

TechBeamers

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python Shutil Module with Examples Shutil Module in Python
Next Article Python Copy Module with Examples Copy Module in Python

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x