How do I clone a list so that it doesn’t change unexpectedly after assignment?

Home /

Table of Contents

Introduction

When working with lists in Python, one common source of confusion for many developers is the behavior of list assignment. Suppose you have a list my_list containing some elements, and you want to create a copy of it to perform operations without affecting the original list. A straightforward approach might be to assign the list to a new variable, let’s say new_list, using new_list = my_list.

However, here comes the catch: any modifications made to new_list will also affect my_list. This behavior can lead to unexpected changes in your data and cause bugs that are difficult to track down.

In this article, we’ll explore why this happens and discuss various methods to properly clone or copy a list in Python, ensuring that modifications to one list do not impact the other.

Understanding Assignment in Python

In Python, when you assign one list to another using the assignment operator (=), you are not creating a new copy of the list. Instead, you are creating a reference to the same list object in memory. This means that both variables will point to the same list object, and any modifications made to one variable will also affect the other.

Let’s illustrate this with an example:

# Original list
my_list = [1, 2, 3, 4, 5]

# Assigning my_list to new_list
new_list = my_list

# Modify new_list
new_list.append(6)

# Print both lists
print("my_list:", my_list)
print("new_list:", new_list)

Shallow Copy vs. Deep Copy

In Python, when you want to create a copy of a list (or any mutable object), you have two options: shallow copy and deep copy. These two methods differ in how they handle nested objects within the list. Let’s delve into the differences:

  1. Shallow Copy:
    • A shallow copy creates a new object but does not create copies of nested objects. Instead, it references the nested objects from the original list.
    • This means that if the original list contains nested mutable objects (like lists or dictionaries), changes to these nested objects will be reflected in both the original list and the shallow copy.
    • Shallow copy is created using methods like slicing, the copy() method, or the list() constructor.
  2. Deep Copy:
    • A deep copy, on the other hand, creates a new object and recursively copies all the elements and nested elements of the original list.
    • This ensures that the new copy is completely independent of the original list, including nested objects.
    • Deep copy is created using the deepcopy() function from the copy module.

Here’s a breakdown of the differences using an example:

import copy

# Original list with nested objects
original_list = [[1, 2, 3], [4, 5, 6]]

# Shallow copy
shallow_copy = copy.copy(original_list)

# Deep copy
deep_copy = copy.deepcopy(original_list)

# Modify the nested object in the original list
original_list[0][0] = 10

# Print all lists
print("Original List:", original_list)
print("Shallow Copy:", shallow_copy)
print("Deep Copy:", deep_copy)

Output:

Original List: [[10, 2, 3], [4, 5, 6]]
Shallow Copy: [[10, 2, 3], [4, 5, 6]]
Deep Copy: [[1, 2, 3], [4, 5, 6]]

Methods for Cloning Lists

Slice Operator:

  • The slice operator [:] can be used to create a shallow copy of a list.
  • When you use [:], it returns a new list containing all the elements from the original list.
  • Since slicing creates a new object, any changes made to the new list will not affect the original list, and vice versa.
  • However, if the list contains nested mutable objects, changes to these objects will be reflected in both the original and the copied list.

Example:

original_list = [1, 2, 3, 4, 5]
shallow_copy = original_list[:]

list() function:

  • The list() function can also be used to create a shallow copy of a list.
  • You pass the original list as an argument to the list() function, and it returns a new list with the same elements.
  • Like slicing, this method creates a new object, so changes made to the new list will not affect the original list.
  • Similarly, changes to nested mutable objects will be reflected in both lists.

Example:

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

deepcopy() function:

  • To create a deep copy of a list, especially when dealing with nested mutable objects, you can use the deepcopy() function from the copy module.
  • This function recursively copies all elements of the original list and any nested lists, dictionaries, etc.
  • It is called as copy.deepcopy(original_list).
  • With deep copy, changes made to nested objects in the copied list will not affect the original list, and vice versa.

Example:

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

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article