How do you copy an object in Python?

Question

Explain its types

in progress 0
Pragya Rani 4 years 2 Answers 940 views Contributor 0

Answers ( 2 )

  1. There are 2 ways to create copies in python
    1) Shallow copy
    2) Deep copy

    In shallow copy, you just create a new collection object and then populate it with
    references to the child objects found in the original list. So, any changes made to the
    copy of the object, do reflect in the original object.

    In Deep copy, it first creates a collection object and then recursively populates it with
    copies of the child objects. So, any changes made to the copy of the object does not result
    to a change in the original object.

    Deep Copy Code Example
    import copy
    l1 = [1,2,3,4,5]
    l2 = copy.deepcopy(l1)

    l2[5] = 6
    print(l1)
    print(l2)

    OUTPUT :
    1 2 3 4 5
    1 2 3 4 5 6

    Shallow Copy Code Example
    import copy
    l1 = [1,2,3,4,5]
    l2 = copy.copy(l1)

    l2[5] = 6
    print(l1)
    print(l2)

    OUTPUT :
    1 2 3 4 5 6
    1 2 3 4 5 6

  2. In Python, when we use = operator to create a copy of an object, It only creates a new variable that shares the reference of the original object and not a new object. So if you modify the values in one object, the same is reflected in the ‘copy’ and both of them share the same ID as well (same memory location). To overcome this, there are two ways to create copies using the copy module: 1. Shallow Copy and 2. Deep Copy

    1. Deep Copy: The copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In case of deep copy, a copy of object is copied in other object

    Example:
    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)
    ——————————————————-
    Old list: [[1, 1, 1], [‘BB’, 2, 2], [3, 3, 3]]
    New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

    2. Shallow Copy: A a new collection object is constructed and then populated with references to the child objects found in the original. The copying process does not recurse and therefore won’t create copies of the child objects themselves. So basically, a reference of object is copied.

    Example1:
    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)
    ————————————————————
    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]]

    Example2: (See the difference between deep and 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)
    ——————————————————————-
    Old list: [[1, 1, 1], [2, ‘AA’, 2], [3, 3, 3]]
    New list: [[1, 1, 1], [2, ‘AA’, 2], [3, 3, 3]]

    In the above program, we made changes to old_list i.e old_list[1][1] = ‘AA’. Both sublists of old_list and new_list at index [1][1] were modified. This is because both lists share the reference of the same nested objects. To prevent this, we can use a deep copy.

Leave an answer

Browse
Browse