Python Shallow Copy and Deep Copy

In this article, you’ll learn about shallow copy and deep copy in Python with the help of examples.

Table of Contents

  • Copy an Object in Python
  • Copy Module
  • Shallow Copy
  • Deep Copy

Copy an Object in Python:


Python, we used the operator = to make a copy of the item. You might think this creates something new; This does not mean that it creates a new trend in the original reference materials.
Let's take an example to make old_list calls and references to the use of new items_list = the employer.
Example 1: Copy number = Operator

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9
print('Old List:', old_list)
print('ID of Old List:', id(old_list))
print('New List:', new_list)
print('ID of New List:', id(new_list))

When we implement the old program, productivity will be:

Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
An ID of Old List: 140673303268168

New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
An ID of New List: 140673303268168

As you can see the normal resources and old_list variables new_list is the same identifier 140673303268168.
Therefore, if you want to have any new_list or old_list, any changes are seen.
Of course, in some cases, you may want to change the original value without the change only if it can change the cost of a new smartphone or the new one. In Python, there are two ways to make a copy:
1. Shallow Copy
2. Deep Copy
Make a copy of this document, and use copy copies.

Copy Module:


We use a Python copy of the surgeon and deeper work. Please, make a copy of the list of enclosures that contain x. For example:

import copy
copy.copy(x)
copy.deepcopy(x)

Shallow Copy:


A copy of the surface creates a new skin store in the original reference part.
Therefore, the above copy will not create duplicate items, instead of the single copy of the referenced objects. This means it is not the way to restore a copy or copy a substance.

Example 2: Create a Copy Using Shallow Copy:


import copy
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
print("Old list:", old_list)
print("New list:", new_list)

When we implement the program, productivity will be:


Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

At the top of the list we have below, a copy of the simplified use of () the copy method.
This means you will have to do some new things with similar things. To check this, print and new_list old_list.

Example 3: Adding [4, 4, 4] to old_list, Using Shallow Copy:


import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.copy(old_list)
old_list.append([4, 4, 4])
print("Old list:", old_list)
print("New list:", new_list)

When we implement the program, it will be:

Old list: [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

The first program we've made in the old words is new_list contains the references of substances originally placed old_list. Next, we add a new list [4, 4, 4] for older people. This new list will be published on a new list.
However, if you change one of the old_list subtitles, the change looks like new_list.

Example 4: Adding a new Nested object using Shallow Copy:


import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.copy(old_list)
old_list[1][1] = 'AA'
print("Old list:", old_list)
print("New list:", new_list)

When we implement the program, it will be:

Old list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]]

Previous program has changed old_list ie old_list [1] [1] 'AA'. The old_list of the new_list in the series [1] [1] has been changed. This is because both share a list of cross-referenced elements.

Deep Copy:


A copy of the creation of a new product that includes copies of the repetitive copies in the first section.
Let's continue with Example 2. However, we do a copy using deeper () fonts in a module. A copy of the creation of a free copy of the original item and all items closed.

Example 5: Copying a List Using Deep copy


import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
print("Old list:", old_list)
print("New list:", new_list)

When we implement the program, it will be:

Old list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

In the program, we use deep () to create a copy that has the same image.
However, if any changes are made to old_list original items, you will see a change that has not been made for a newer version.

Example 6: Adding a new Nested Object in the list Using Deep Copy


import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
old_list[1][0] = 'BB'
print("Old list:", old_list)
print("New list:", new_list)

When we implement the program, it will be:

Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]


In the first program, when we set the old_list value, we can only see that old_list is changed. This means both new_list old_list is free. This is due to the fact that the oldest recyclables, which are essentially the cosmetic products, are made.

To getting expect level training for Python training in your Location – Python training in Chennai | Python training institute in Chennai | Python Course institute in Chennai | Python training in Bangalore | Python training institute in Bangalore Python training in Electronic City Python training in Jaya Nagar | Python training in Pune | Python training institute in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram Python training in Annanagar | Python training in Marathahalli Python training in Btm | Python Online Training | Online Certification Course Python Course in Chennai Python interview questions and answers | Python tutorials | Python training in Indira Nagar | Python Course in Btm | Python Course in Marathahalli Python training institute in Marathahalli Python course institute in Btm Python course in Electronic City | Python online training in Chennai | Python online training in Bangalore | Python Interview Questions & Answers .


Comments

  1. Hey amigos, it is incredible composed piece completely characterized, proceed with the great work continually.
    AI course malaysia

    ReplyDelete
  2. Learn the full knowledge of Python programming by registering for an AI Patasala Advanced Python Training in Hyderabad.
    Online Python Course in Hyderabad

    ReplyDelete
  3. Acquire comprehensive expertise in Python programming by enrolling in the Advanced Python Training at AchieversIT in Marathahalli.

    python training in marathahalli

    python training in marathahalli

    ReplyDelete

Post a Comment

Popular posts from this blog

Python – Functions

Python Global Keyword