Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Shallow copy and deep copy in Python

In Python, when working with objects, sometimes you may want to create a copy of the object. There are two types of copies you can create: shallow copy and deep copy.

1. Shallow copy:

A shallow copy creates a new object, but instead of copying the nested objects, it only copies the references to those objects. This means that the new object and the original object share the same nested objects. Any changes made to the nested objects will be reflected in both the original and the copied objects.

You can create a shallow copy using the copy() method or the copy module's copy() function.

Here's an example of a shallow copy:

import copy

original_list = [1, 2, [3, 4]]
shallow_copy = copy.copy(original_list)

shallow_copy[2][0] = 99

print("Original list:", original_list)      # Output: Original list: [1, 2, [99, 4]]
print("Shallow copy:", shallow_copy)        # Output: Shallow copy: [1, 2, [99, 4]]

In this example, modifying the nested list in the shallow_copy also affects the original_list, as both lists share the same nested object.

2. Deep copy:

A deep copy creates a new object, and recursively copies all the nested objects as well. This means that the new object and the original object have no shared nested objects. Any changes made to the nested objects in one will not affect the other.

You can create a deep copy using the copy module's deepcopy() function.

Here's an example of a deep copy:

import copy

original_list = [1, 2, [3, 4]]
deep_copy = copy.deepcopy(original_list)

deep_copy[2][0] = 99

print("Original list:", original_list)      # Output: Original list: [1, 2, [3, 4]]
print("Deep copy:", deep_copy)              # Output: Deep copy: [1, 2, [99, 4]]

In this example, modifying the nested list in the deep_copy does not affect the original_list, as the deep copy has its own independent copy of the nested object.

In summary, when copying objects in Python, you can create either a shallow copy or a deep copy. A shallow copy shares nested objects between the original and the copy, while a deep copy creates independent copies of all nested objects. Choose the appropriate type of copy based on your specific needs.

  1. How to Perform Shallow Copy in Python:

    • Shallow copy creates a new object but doesn't create copies of nested objects.
    • The copy module's copy() function is used for shallow copy.
    import copy
    
    original_list = [1, [2, 3], 4]
    shallow_copy_list = copy.copy(original_list)
    
  2. Deep Copy Implementation in Python:

    • Deep copy creates a new object and recursively copies all nested objects.
    • The copy module's deepcopy() function is used for deep copy.
    import copy
    
    original_list = [1, [2, 3], 4]
    deep_copy_list = copy.deepcopy(original_list)
    
  3. Python copy.deepcopy() Function:

    • The deepcopy() function in the copy module creates a deep copy of an object.
    import copy
    
    original_dict = {'a': 1, 'b': {'c': 2}}
    deep_copy_dict = copy.deepcopy(original_dict)
    
  4. Mutable and Immutable Objects in Copy Operations:

    • Shallow and deep copy affect mutable and immutable objects differently.
    • Changes to mutable objects impact both the original and shallow copy.
    • Changes to immutable objects don't affect the original or shallow copy.
    import copy
    
    original_list = [1, 2, 3]
    shallow_copy_list = copy.copy(original_list)
    
    original_list[0] = 10  # Changes affect both original and shallow copy
    
  5. Python copy Module Examples:

    • The copy module provides various functions for different copy operations, including copy(), deepcopy(), and more.
    import copy
    
    original_list = [1, [2, 3], 4]
    
    # Shallow copy
    shallow_copy_list = copy.copy(original_list)
    
    # Deep copy
    deep_copy_list = copy.deepcopy(original_list)