How to Clone or Copy a List to Avoid Unexpected Changes in Python?

How to Clone or Copy a List to Avoid Unexpected Changes in Python?

It is important to clone a list to ensure that changes to the new list do not alter the original, especially when the lists contain mutable objects like other lists or dictionaries. In this article, we’ll discuss various methods that can be used to clone the list or copy the list properly. 

Table of Contents:

Methods to Clone or Copy a List to Avoid Unexpected Changes in Python

Cloning or copying a list is essential if you want to keep your original data unaltered. Python provides various methods to clone or copy a list without unexpected changes:

Method 1: Using copy() method to clone a list in Python 

In Python copy() method is a built-in function to copy the elements in the list instead of modifying the original list it creates a new list. 

Example: 

Python

Output:

Explanation: The copy() method copies the outer list but the elements in the inner list elements are not copied which is the same as shallow copy(), so copy() and shallow copy methods are the same.

Method 2: Using shallow copy() method to clone a list in Python 

This method creates a “shallow copy()” of the list. The meaning of shallow copy() it creates a new object with references to the original data means it only copies the outer list but the inner list is unchangeable. Changes to the copied object affect the original.

Example: 

Python

Output: 

Explanation: Here, shallow_copy = original.copy() creates a shallow copy of the original means the outer list is only copied but the inert list [4,5] is not copied. Shallow_copy[1] refers to the inner list[4,5] and while modifying the shallow_copy[1][1], we’re changing the second element of the inner list from 5 to 99.

Method 3: Using deep copy() method to clone a list in Python 

This method creates a “deep copy()” of the list. This means the deep copy() creates a completely new and independent object unlike shallow copy deep copy() doesn’t affect the original list means the original and the copied lists are completely independent of each other.

Example: 

Python

Output: 

Explanation: Here,deep_copy = copy.deepcopy(original) creates an independent list deep_copy[1] refers to the inner list[4,5] and while modifying the deep_copy[1][1] it changes only the inner list and the outer list doesn’t get changed.

Method 4: Using list slicing() method to clone a list in Python 

In Python, the slicing() method is another way to create a shallow copy of a list, it only copies the outer list but it doesn’t copy the nested objects in the list. 

Example: 

Python

Output: 

Explanation: Here the slicing is applied to the original list which is copy_list = original_list[:] we didn’t declare the start, stop, and end for the list, so it prints all the elements in the list.

Note: slicing works better for immutable (strings, numbers, etc) but for mutable objects(lists, dictionaries), it only copies the references of the object.

Method 5: Using copy.copy() method to clone a list in Python 

The copy.copy() function in Python creates a shallow copy list, where it only copies the outer list but it doesn’t copy the nested objects in the list. 

Example:

Python

Output: 

Explanation: Here, is the copy.copy() method is used to create a shallow copy of the original list i.e., it copies the original list but not the nested lists. It also changes the original list because both lists share references to the same nested list.

Method 6: Using list() constructor to clone a list in Python 

The list() constructor method  is used to create a copy from the original list 

Example: 

Python

Output:

Explanation: Here, the list() constructor method is used to create a new list, copied_list() that is a copy of the original list. Here, the copied list won’t affect the original list unless nested objects are involved.

Method 7: Using list comprehension to clone the list in Python 

The list comprehension method to clone the original list without making any changes to the original list.

Example: 

Python

Output: 

Explanation: In this example, The list comprehension method creates a new list, copied_list, by copying each item from the original_list. Changes to the copied list won’t affect the original list unless there are nested objects.

Conclusion

To clone a list in Python, we can use either shallow or deep copy methods. Shallow copy() is created with methods like copy(), copy.copy(), or list.slicing(). These methods only copy the outer objecṭs and don’t change the nested objects.Alternatively, a deep copy created using copy.deepcopy() creates completely independent copies, including nested objects. Other methods like List comprehension and list() are used to clone a list from the original list. Understanding these approaches is important to ensure that changes to the new list do not alter the original list.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner