Course
Python lists are powerful data structures used to store a collection of items. The copy list method is important when handling data since it helps preserve the original list while performing operations on the duplicated one. In this tutorial, I will explain the basic and advanced techniques of copying lists with practical examples.
Before exploring the different examples, I recommend taking DataCamp’s Introduction to Python course to learn the basics of Python, including the different Python data structures. The Python Cheat Sheet for Beginners will also be helpful for referencing different functions and methods.
"Python using a copy machine." Image by Dall-E.
Quick Answer: How to Copy a List in Python
The example below shows how to copy a list in Python using the copy()
method. Notice how the original list remains unchanged even though changes are made to the duplicate list. The code will print [1, 2, 3]
.
# Define the original list
original_list = [1, 2, 3]
# Create a shallow copy of the original list
copied_list = original_list.copy()
# Modify the first element of the copied list
copied_list[0] = 99
print(original_list)
[1, 2, 3]
More Techniques to Copy a List in Python
Before we look at the different techniques for copying a list in Python, let’s consider why we don’t use the assignment operator =
. We don’t use =
because doing so would not actually create a copy of our list. The assignment operator would only create a reference to our list.
In the following example, original_list
and reference_to_original_list
refer to the same list after our assignment.
# Create a list called original_list
original_list = [1, 2, 3]
# Create a new reference copied_list that points to the same list as original_list
reference_to_original_list = original_list
Here, any modifications on the duplicated list will affect the original list and vice versa. These changes occur since the two lists are stored in the same memory. Now, let’s take a look at the different ways to actually copy our list.
Using the copy() method
The copy()
method is a built-in method used to create a shallow copy of the list. The copy()
method is available from Python 3.3 onwards. This method copies only the top-level list structure. The syntax of the copy()
method is shown below.
new_list = original_list.copy()
In the following example, the code will print [1, 2, 3, 4]
.
# Original list with nested structure
original_list = [1, 2, 3, 4]
# Create a shallow copy using the copy() method
shallow_copied_list = original_list.copy()
print(shallow_copied_list)
[1, 2, 3, 4]
Using the list() constructor
The list()
constructor is used to create a new list from an existing list to include all the elements from the original list. This technique is important when creating a new list object where changes in the copied list will not affect the original list. The following code prints [1, 2, 3]
.
# Create a list called original_list
original_list = [1, 2, 3]
# Create a shallow copy of original_list using the list() function
copied_list = list(original_list)
# Modify the first element of copied_list
copied_list[0] = 99
print(original_list)
[1, 2, 3]
Using list slicing with [:]
List slicing [:]
provides a concise method of creating a shallow copy of a list. The list slicing [:]
method is efficient since it is implemented at a low level in Python. Below is the syntax of this method.
new_list = original_list[:]
The following code prints [1, 2, 3]
.
# Create a list called original_list
original_list = [1, 2, 3]
# Create a shallow copy of original_list using list slicing
copied_list = original_list[:]
# Modify the first element of copied_list
copied_list[0] = 99
print(original_list)
[1, 2, 3]
In this context, we use the list slicing method to include everything in the list, but in other contexts, slicing can be used to create sublists by specifying start and end indices.
Become an ML Scientist
Python Shallow Copy vs. Deep Copy
Understanding the difference between shallow and deep copy is important to prevent unintended data transformation.
- A shallow copy copies only the top-level elements of the list and does not affect the nested lists or mutable objects within the list that reference the same objects. The changes made in the mutable objects will affect both the original and copied lists.
- Deep copy recursively copies all elements, including nested lists and mutable objects, creating independent copies. The changes made in the nested objects in the copied list will not affect the original list.
I recommend using the copy.copy()
function for shallow copying and copy.deepcopy()
function for deep copying.
Method |
Shallow Copy |
Deep Copy |
copy() |
✓ |
✗ |
list() constructor |
✓ |
✗ |
List slicing [:] |
✓ |
✗ |
copy.copy() |
✓ |
✗ |
copy.deepcopy() |
✗ |
✓ |
How to Copy a Nested List in Python
When handling nested lists or complex data structures, more sophisticated list copying methods are required. Let us look at the different techniques for handling nested lists.
Using copy.copy()
The copy
module provides the copy()
function which creates a shallow copy of a list. The copy()
function is more explicit than using the list()
constructor or list slicing [:]
. You can use this method when you want to emphasize creating a shallow copy of a list to improve readability.
The example below shows how to shallow copy a nested list. The code will print [1, 2, [99, 4]]
because the nested list is still the same object.
# Import the copy module to access the copy.copy() function
import copy
# Original list with nested structure
original_list = [1, 2, [3, 4]]
# Create a shallow copy using copy.copy()
shallow_copied_list = copy.copy(original_list)
# Modifying the nested list in the copied list
shallow_copied_list[2][0] = 99
print(original_list)
[1, 2, [99, 4]]
Using copy.deepcopy()
The deepcopy()
function is also available in the copy
module. This function creates a deep copy of the list, duplicating the list itself and all nested objects. The deepcopy()
function is important when working with mutable objects within lists because it ensures changes in the copied lists do not affect the original list.
The code below prints [1, 2, [3, 4]]
.
# Import the copy module to access the copy.deepcopy() function
import copy
# Original list with nested structure
original_list = [1, 2, [3, 4]]
# Create a deep copy using copy.deepcopy()
deep_copied_list = copy.deepcopy(original_list)
# Modifying the nested list in the copied list
deep_copied_list[2][0] = 99
print(original_list)
[1, 2, [3, 4]]
Conclusion
Understanding how to copy lists in Python is important for efficient data manipulation. In this tutorial, we learned different methods for copying a list in Python and explored some of the differences between shallow copy and deep copy methods. As a data analyst, choosing the appropriate list copy technique based on your specific requirements is important. I encourage you to practice the methods discussed to better understand their use cases.
Take DataCamp’s Python Programming skill track to advance your programming skills with the best techniques. I also encourage you to check out our Python Developer and Data Analyst with Python career tracks to help you keep building practical, job-relevant skills.
Become a ML Scientist
Frequently Asked Questions
What is the difference between shallow copy and deep copy?
The shallow copy method creates a new list while referencing it with the same values as the original list such that changes in the objects in the copied list affect the original list. The deep copy creates a new list and recursively inserts the values from the original list, so changes to nested objects in the copied list do not affect the original list.
What are the methods for creating a shallow copy in Python?
You can create a shallow copy of a list using the copy()
method, list slicing [:]
, the list()
constructor, or the copy()
function from the copy
module.
How do you create a deep copy of a list in Python?
You can create a deep copy of a list using the deepcopy()
function from the copy
module.