How Can You Copy Objects in Python?

In Python, if you want to copy an object, the assignment operator won't fulfill the purpose. It creates bindings between a target and an object, meaning it never creates a new object ? it only creates a new variable sharing the reference of the original object. To fix this, Python provides the copy module with generic shallow and deep copy operations.

Assignment vs Copy Operations

Let's first understand why assignment doesn't create a copy ?

original = [[1, 2], [3, 4]]
assigned = original

# Modifying assigned affects original
assigned[0][0] = 99
print("Original:", original)
print("Assigned:", assigned)
Original: [[99, 2], [3, 4]]
Assigned: [[99, 2], [3, 4]]

Shallow Copy

A shallow copy constructs a new compound object and then inserts references into it to the objects found in the original. It uses copy.copy(x) to create a shallow copy.

import copy

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

print("Original:", original)
print("Shallow copy:", shallow_copied)

# Modifying inner objects affects both
shallow_copied[0][0] = 99
print("After modifying shallow copy:")
print("Original:", original)
print("Shallow copy:", shallow_copied)
Original: [[1, 2], [3, 4]]
Shallow copy: [[1, 2], [3, 4]]
After modifying shallow copy:
Original: [[99, 2], [3, 4]]
Shallow copy: [[99, 2], [3, 4]]

Deep Copy

A deep copy constructs a new compound object and then recursively inserts copies into it of the objects found in the original. It uses copy.deepcopy(x) to create a deep copy.

import copy

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

print("Original:", original)
print("Deep copy:", deep_copied)

# Modifying inner objects doesn't affect original
deep_copied[0][0] = 99
print("After modifying deep copy:")
print("Original:", original)
print("Deep copy:", deep_copied)
Original: [[1, 2], [3, 4]]
Deep copy: [[1, 2], [3, 4]]
After modifying deep copy:
Original: [[1, 2], [3, 4]]
Deep copy: [[99, 2], [3, 4]]

Comparison

Method Creates New Object? Copies Nested Objects? Best For
Assignment No No Reference sharing
Shallow Copy Yes No (references only) Simple nested structures
Deep Copy Yes Yes (recursive) Complex nested structures

Copying Custom Objects

The copy module also works with custom classes ?

import copy

class Person:
    def __init__(self, name, hobbies):
        self.name = name
        self.hobbies = hobbies

original_person = Person("Alice", ["reading", "coding"])

# Shallow copy
shallow_person = copy.copy(original_person)
shallow_person.hobbies.append("gaming")

# Deep copy
deep_person = copy.deepcopy(original_person)
deep_person.hobbies.append("painting")

print("Original hobbies:", original_person.hobbies)
print("Shallow copy hobbies:", shallow_person.hobbies)
print("Deep copy hobbies:", deep_person.hobbies)
Original hobbies: ['reading', 'coding', 'gaming']
Shallow copy hobbies: ['reading', 'coding', 'gaming']
Deep copy hobbies: ['reading', 'coding', 'gaming', 'painting']

Conclusion

Use copy.copy() for shallow copying when you need a new object but can share references to nested objects. Use copy.deepcopy() when you need complete independence between original and copied objects. The copy module is built into Python, so no installation is required.

Updated on: 2026-03-26T21:32:41+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements