in Technology by
How do you copy an object in Python?

1 Answer

0 votes
by

In Python, the assignment statement (= operator) does not copy objects. Instead, it creates a binding between the existing object and the target variable name. To create copies of an object in Python, we need to use the copy module. Moreover, there are two ways of creating copies for the given object using the copy module -

  • Shallow Copy is a bit-wise copy of an object. The copied object created has an exact copy of the values in the original object. If either of the values are references to other objects, just the reference addresses for the same are copied.
  • Deep Copy copies all values recursively from source to target object, i.e. it even duplicates the objects referenced by the source object.
from copy import copy, deepcopy

list_1 = [1, 2, [3, 5], 4]

## shallow copy

list_2 = copy(list_1) 
list_2[3] = 7
list_2[2].append(6)

list_2    # output => [1, 2, [3, 5, 6], 7]
list_1    # output => [1, 2, [3, 5, 6], 4]

## deep copy

list_3 = deepcopy(list_1)
list_3[3] = 8
list_3[2].append(7)

list_3    #

Related questions

0 votes
    It's a bit difficult to implement a deep object copy function. What steps you take to ensure the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    How to copy a file in python? I already read this but found nothing. Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I clone or deep copy an object so that the cloned object can be modified without any changes being reflected in the original object?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    How will you remove an object from a list in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    How will you convert a String to an object in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    I have 7 excel sheets in one workbook and I am trying to copy and paste the data from each excel ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Suman asked her friend Niharika her English Copy. However she didn't gave suman. What do you think might ... behind this communication. Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    Suman asked her friend Niharika her English Copy. However she didn't gave Suman. What do you think might ... behind this communication. Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    What do you understand by copy constructor in Java?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    How does one write a unittest that fails only if a function doesn't throw an expected exception? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How will you remove last object from a list in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    How will you convert a object to a regular expression in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    How will you convert a object to a string in python?...
asked Nov 24, 2020 in Technology by JackTerrance
0 votes
    The following code from Bacon.js will push the data object returned from the AJAX request into the console. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    Fill in the blanks :- i. ____ means that the object is inserted as a copy of the original document. ii. ... to browse the internet. Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
...