Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 31

Explain the difference between shallow copy and deep copy.

Answer:

In Python, copying an object can be done in two ways: shallow copy and deep copy. Both are used to create a new object that is a copy of an existing object, but they differ in how they handle the nested objects (objects containing other objects).

Shallow Copy

A shallow copy creates a new object, but does not create copies of nested objects. Instead, it copies the references to the original objects into the new object. Therefore, the new object and the original object share the nested objects.

How to Create a Shallow Copy

  • Using copy module:

    import copy
    
    original_list = [1, 2, [3, 4]]
    shallow_copied_list = copy.copy(original_list)
  • Using list slicing (for lists only):

    original_list = [1, 2, [3, 4]]
    shallow_copied_list = original_list[:]
  • Using the list() function (for lists only):

    original_list = [1, 2, [3, 4]]
    shallow_copied_list = list(original_list)

Example of Shallow Copy:

import copy

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

# Modifying the nested list in the shallow copy
shallow_copy[2][0] = 99

print("Original:", original)      # Output: Original: [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 modifies the original list because both lists share the same nested objects.

Deep Copy

A deep copy creates a new object and recursively copies all objects found in the original object. This means that the new object and the original object do not share any nested objects.

How to Create a Deep Copy

  • Using copy module:

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

Example of Deep Copy:

import copy

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

# Modifying the nested list in the deep copy
deep_copy[2][0] = 99

print("Original:", original)      # Output: Original: [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 because the nested objects are completely independent.

Summary

  • Shallow Copy:

    • Creates a new object but inserts references into it to the objects found in the original.
    • The new object and the original object share the same nested objects.
    • Suitable for flat or simple objects without nested structures.
    • Methods: copy.copy(), list slicing, list(), etc.
  • Deep Copy:

    • Creates a new object and recursively copies all objects found in the original.
    • The new object and the original object do not share any nested objects.
    • Suitable for complex objects with nested structures.
    • Method: copy.deepcopy().

Understanding the difference between shallow copy and deep copy is important for preventing unintended side effects when copying complex objects in Python.

Recent job openings