Python List copy() method [With Examples]

In this Python tutorial, we will discuss the Python List copy() method. It is an essential method of the list class that allows us to create a copy of a given Python list.

List copy() method in Python

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python List copy() method
  • Syntax of the copy() method
  • Python Shallow Copy vs Python Deep Copy
  • Example of the copy() method

Python List copy() method

The list copy() method creates a shallow copy of a given Python list. A shallow copy means that the new list will have its own reference in memory but will share references for mutable elements, like nested lists.

The syntax for the copy() method is as follows:

new_list = old_list.copy()

Python Shallow Copy vs Deep Copy

The following is the difference between the Shallow Copy and Deep Copy in Python.

Python Shallow CopyPython Deep Copy
In Python, shallow copy creates a new list, but it does not create new objects for the mutable elements within the Python list.In Python, deep copy creates a new Python list and also creates new objects for all the mutable elements within the Python list.
It shares the references of the original Python list’s mutable elements.It ensures that changes made to the original Python list do not affect the new Python list, and vice versa.
For this, Python has a built-in copy() function.For this, we have to import the copy module first, then we are able to use the deepcopy() method.

copy() method in Python List Examples

Let’s dive into some examples to see the copy() method in action.

Example#1 Creating a Shallow Copy

original_states = ['California', 'New York', 'Texas', 'Florida', 'Illinois']
copied_states = original_states.copy()

print("Original States:", original_states)
print("Copied States:", copied_states)

In this example, we have an original Python list original_states containing five US state names. We then create a new Python list copied_states using the copy() method.

When we print both Python lists, we can see that they contain the same elements, indicating that the Python copy() method has successfully created a shallow copy of the original list.

Output:

Python List copy method
Python List copy method

Example#2 Demonstrating Shared References in Shallow Copy

original_cities = ['Los Angeles', 'New York', ['Houston', 'Dallas'], 'Miami']
copied_cities = original_cities.copy()

original_cities[2][0] = 'Austin'
print("Original Cities:", original_cities)
print("Copied Cities:", copied_cities)

In this example, we have an original Python list original_cities containing US city names, including a nested Python list with two city names. We create a new Python list copied_cities using the copy() method.

Then, we modify the original Python list by changing the first city in the nested list from ‘Houston’ to ‘Austin’. When we print both Python lists, we can see that the change in the original list is also reflected in the copied list.

This is because the Python list copy() method creates a shallow copy that shares references to mutable elements, such as nested Python lists. As a result, changes made to mutable elements in the original list will also affect the copied list.

Output:

Python List copy method example
Python List copy method example

Conclusion

In this article, we covered the Python list copy() method, its usage, and its importance when working with lists. We also discussed the difference between shallow and deep copies and demonstrated the behavior of the copy() method using examples.

You may also like to read the following articles: